ole32: Use a snapshot file when sharing storages for writing.
[wine/multimedia.git] / dlls / dinput / joystick_linuxinput.c
blob76adabc25398564c25f0a6942233a752032561e0
1 /* DirectInput Joystick device
3 * Copyright 1998,2000 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2005 Daniel Remenak
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <time.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h>
36 #endif
37 #include <fcntl.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 # include <sys/ioctl.h>
40 #endif
41 #include <errno.h>
42 #ifdef HAVE_LINUX_INPUT_H
43 # include <linux/input.h>
44 # undef SW_MAX
45 # if defined(EVIOCGBIT) && defined(EV_ABS) && defined(BTN_PINKIE)
46 # define HAS_PROPER_HEADER
47 # endif
48 #endif
49 #ifdef HAVE_SYS_POLL_H
50 # include <sys/poll.h>
51 #endif
53 #include "wine/debug.h"
54 #include "wine/unicode.h"
55 #include "wine/list.h"
56 #include "windef.h"
57 #include "winbase.h"
58 #include "winerror.h"
59 #include "winreg.h"
60 #include "dinput.h"
62 #include "dinput_private.h"
63 #include "device_private.h"
64 #include "joystick_private.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
68 #ifdef HAS_PROPER_HEADER
70 #define EVDEVPREFIX "/dev/input/event"
71 #define EVDEVDRIVER " (event)"
73 /* Wine joystick driver object instances */
74 #define WINE_JOYSTICK_MAX_AXES 8
75 #define WINE_JOYSTICK_MAX_POVS 4
76 #define WINE_JOYSTICK_MAX_BUTTONS 128
78 struct wine_input_absinfo {
79 LONG value;
80 LONG minimum;
81 LONG maximum;
82 LONG fuzz;
83 LONG flat;
86 /* implemented in effect_linuxinput.c */
87 HRESULT linuxinput_create_effect(int* fd, REFGUID rguid, struct list *parent_list_entry, LPDIRECTINPUTEFFECT* peff);
88 HRESULT linuxinput_get_info_A(int fd, REFGUID rguid, LPDIEFFECTINFOA info);
89 HRESULT linuxinput_get_info_W(int fd, REFGUID rguid, LPDIEFFECTINFOW info);
91 typedef struct JoystickImpl JoystickImpl;
92 static const IDirectInputDevice8AVtbl JoystickAvt;
93 static const IDirectInputDevice8WVtbl JoystickWvt;
95 struct JoyDev {
96 char *device;
97 char *name;
98 GUID guid;
100 BOOL has_ff;
101 int num_effects;
103 /* data returned by EVIOCGBIT for caps, EV_ABS, EV_KEY, and EV_FF */
104 BYTE evbits[(EV_MAX+7)/8];
105 BYTE absbits[(ABS_MAX+7)/8];
106 BYTE keybits[(KEY_MAX+7)/8];
107 BYTE ffbits[(FF_MAX+7)/8];
109 /* data returned by the EVIOCGABS() ioctl */
110 struct wine_input_absinfo axes[ABS_MAX];
112 WORD vendor_id, product_id;
115 struct JoystickImpl
117 struct JoystickGenericImpl generic;
118 struct JoyDev *joydev;
120 /* joystick private */
121 int joyfd;
123 int dev_axes_to_di[ABS_MAX];
124 POINTL povs[4];
126 /* LUT for KEY_ to offset in rgbButtons */
127 BYTE buttons[KEY_MAX];
129 /* Force feedback variables */
130 struct list ff_effects;
131 int ff_state;
132 int ff_autocenter;
133 int ff_gain;
136 static inline JoystickImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
138 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface),
139 JoystickGenericImpl, base), JoystickImpl, generic);
141 static inline JoystickImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
143 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface),
144 JoystickGenericImpl, base), JoystickImpl, generic);
146 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(JoystickImpl *This)
148 return &This->generic.base.IDirectInputDevice8A_iface;
150 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickImpl *This)
152 return &This->generic.base.IDirectInputDevice8W_iface;
155 static void fake_current_js_state(JoystickImpl *ji);
156 static void find_joydevs(void);
157 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface);
159 /* This GUID is slightly different from the linux joystick one. Take note. */
160 static const GUID DInput_Wine_Joystick_Base_GUID = { /* 9e573eda-7734-11d2-8d4a-23903fb6bdf7 */
161 0x9e573eda,
162 0x7734,
163 0x11d2,
164 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
167 #define test_bit(arr,bit) (((BYTE*)(arr))[(bit)>>3]&(1<<((bit)&7)))
169 #define MAX_JOYDEV 64
171 static int have_joydevs = -1;
172 static struct JoyDev *joydevs = NULL;
174 static void find_joydevs(void)
176 int i;
178 if (InterlockedCompareExchange(&have_joydevs, 0, -1) != -1)
179 /* Someone beat us to it */
180 return;
182 for (i = 0; i < MAX_JOYDEV; i++)
184 char buf[MAX_PATH];
185 struct JoyDev joydev = {0};
186 int fd;
187 BOOL no_ff_check = FALSE;
188 int j;
189 struct JoyDev *new_joydevs;
190 struct input_id device_id = {0};
192 snprintf(buf, sizeof(buf), EVDEVPREFIX"%d", i);
194 if ((fd = open(buf, O_RDWR)) == -1)
196 fd = open(buf, O_RDONLY);
197 no_ff_check = TRUE;
200 if (fd == -1)
202 WARN("Failed to open \"%s\": %d %s\n", buf, errno, strerror(errno));
203 continue;
206 if (ioctl(fd, EVIOCGBIT(0, sizeof(joydev.evbits)), joydev.evbits) == -1)
208 WARN("ioctl(EVIOCGBIT, 0) failed: %d %s\n", errno, strerror(errno));
209 close(fd);
210 continue;
212 if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(joydev.absbits)), joydev.absbits) == -1)
214 WARN("ioctl(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno, strerror(errno));
215 close(fd);
216 continue;
218 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(joydev.keybits)), joydev.keybits) == -1)
220 WARN("ioctl(EVIOCGBIT, EV_KEY) failed: %d %s\n", errno, strerror(errno));
221 close(fd);
222 continue;
225 /* A true joystick has at least axis X and Y, and at least 1
226 * button. copied from linux/drivers/input/joydev.c */
227 if (!test_bit(joydev.absbits, ABS_X) || !test_bit(joydev.absbits, ABS_Y) ||
228 !(test_bit(joydev.keybits, BTN_TRIGGER) ||
229 test_bit(joydev.keybits, BTN_A) ||
230 test_bit(joydev.keybits, BTN_1)))
232 close(fd);
233 continue;
236 if (!(joydev.device = HeapAlloc(GetProcessHeap(), 0, strlen(buf) + 1)))
238 close(fd);
239 continue;
241 strcpy(joydev.device, buf);
243 buf[MAX_PATH - 1] = 0;
244 if (ioctl(fd, EVIOCGNAME(MAX_PATH - 1), buf) != -1 &&
245 (joydev.name = HeapAlloc(GetProcessHeap(), 0, strlen(buf) + strlen(EVDEVDRIVER) + 1)))
247 strcpy(joydev.name, buf);
248 /* Append driver name */
249 strcat(joydev.name, EVDEVDRIVER);
251 else
252 joydev.name = joydev.device;
254 if (device_disabled_registry(joydev.name)) {
255 close(fd);
256 HeapFree(GetProcessHeap(), 0, joydev.name);
257 if (joydev.name != joydev.device)
258 HeapFree(GetProcessHeap(), 0, joydev.device);
259 continue;
262 joydev.guid = DInput_Wine_Joystick_Base_GUID;
263 joydev.guid.Data3 += have_joydevs;
265 TRACE("Found a joystick on %s: %s (%s)\n",
266 joydev.device, joydev.name,
267 debugstr_guid(&joydev.guid)
270 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
271 if (!no_ff_check &&
272 test_bit(joydev.evbits, EV_FF) &&
273 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(joydev.ffbits)), joydev.ffbits) != -1 &&
274 ioctl(fd, EVIOCGEFFECTS, &joydev.num_effects) != -1 &&
275 joydev.num_effects > 0)
277 TRACE(" ... with force feedback\n");
278 joydev.has_ff = TRUE;
280 #endif
282 for (j = 0; j < ABS_MAX;j ++)
284 if (!test_bit(joydev.absbits, j)) continue;
285 if (ioctl(fd, EVIOCGABS(j), &(joydev.axes[j])) != -1)
287 TRACE(" ... with axis %d: cur=%d, min=%d, max=%d, fuzz=%d, flat=%d\n",
289 joydev.axes[j].value,
290 joydev.axes[j].minimum,
291 joydev.axes[j].maximum,
292 joydev.axes[j].fuzz,
293 joydev.axes[j].flat
298 if (ioctl(fd, EVIOCGID, &device_id) == -1)
299 WARN("ioctl(EVIOCGID) failed: %d %s\n", errno, strerror(errno));
300 else
302 joydev.vendor_id = device_id.vendor;
303 joydev.product_id = device_id.product;
306 if (!have_joydevs)
307 new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
308 else
309 new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joydevs, (1 + have_joydevs) * sizeof(struct JoyDev));
311 if (!new_joydevs)
313 close(fd);
314 continue;
316 joydevs = new_joydevs;
317 joydevs[have_joydevs] = joydev;
318 have_joydevs++;
320 close(fd);
324 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
326 DWORD dwSize = lpddi->dwSize;
328 TRACE("%d %p\n", dwSize, lpddi);
329 memset(lpddi, 0, dwSize);
331 lpddi->dwSize = dwSize;
332 lpddi->guidInstance = joydevs[id].guid;
333 lpddi->guidProduct = DInput_Wine_Joystick_Base_GUID;
334 lpddi->guidFFDriver = GUID_NULL;
336 if (version >= 0x0800)
337 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
338 else
339 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
341 strcpy(lpddi->tszInstanceName, joydevs[id].name);
342 strcpy(lpddi->tszProductName, joydevs[id].name);
345 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
347 DWORD dwSize = lpddi->dwSize;
349 TRACE("%d %p\n", dwSize, lpddi);
350 memset(lpddi, 0, dwSize);
352 lpddi->dwSize = dwSize;
353 lpddi->guidInstance = joydevs[id].guid;
354 lpddi->guidProduct = DInput_Wine_Joystick_Base_GUID;
355 lpddi->guidFFDriver = GUID_NULL;
357 if (version >= 0x0800)
358 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
359 else
360 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
362 MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
363 MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszProductName, MAX_PATH);
366 static HRESULT joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
368 find_joydevs();
370 if (id >= have_joydevs) {
371 return E_FAIL;
374 if (!((dwDevType == 0) ||
375 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
376 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
377 return S_FALSE;
379 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
380 if (dwFlags & DIEDFL_FORCEFEEDBACK)
381 return S_FALSE;
382 #endif
384 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
385 fill_joystick_dideviceinstanceA(lpddi, version, id);
386 return S_OK;
388 return S_FALSE;
391 static HRESULT joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
393 find_joydevs();
395 if (id >= have_joydevs) {
396 return E_FAIL;
399 if (!((dwDevType == 0) ||
400 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
401 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
402 return S_FALSE;
404 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
405 if (dwFlags & DIEDFL_FORCEFEEDBACK)
406 return S_FALSE;
407 #endif
409 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
410 fill_joystick_dideviceinstanceW(lpddi, version, id);
411 return S_OK;
413 return S_FALSE;
416 static JoystickImpl *alloc_device(REFGUID rguid, IDirectInputImpl *dinput, unsigned short index)
418 JoystickImpl* newDevice;
419 LPDIDATAFORMAT df = NULL;
420 int i, idx = 0;
421 int default_axis_map[WINE_JOYSTICK_MAX_AXES + WINE_JOYSTICK_MAX_POVS*2];
423 newDevice = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(JoystickImpl));
424 if (!newDevice) return NULL;
426 newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
427 newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
428 newDevice->generic.base.ref = 1;
429 newDevice->generic.base.guid = *rguid;
430 newDevice->generic.base.dinput = dinput;
431 newDevice->generic.joy_polldev = joy_polldev;
432 newDevice->joyfd = -1;
433 newDevice->joydev = &joydevs[index];
434 newDevice->generic.name = newDevice->joydev->name;
435 list_init(&newDevice->ff_effects);
436 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
437 newDevice->ff_state = FF_STATUS_STOPPED;
438 #endif
439 /* There is no way in linux to query force feedback autocenter status.
440 Instead, track it with ff_autocenter, and assume it's initially
441 enabled. */
442 newDevice->ff_autocenter = 1;
443 newDevice->ff_gain = 0xFFFF;
444 InitializeCriticalSection(&newDevice->generic.base.crit);
445 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
447 /* Count number of available axes - supported Axis & POVs */
448 for (i = 0; i < ABS_MAX; i++)
450 if (i < WINE_JOYSTICK_MAX_AXES &&
451 test_bit(newDevice->joydev->absbits, i))
453 newDevice->generic.device_axis_count++;
454 newDevice->dev_axes_to_di[i] = idx;
455 newDevice->generic.props[idx].lDevMin = newDevice->joydev->axes[i].minimum;
456 newDevice->generic.props[idx].lDevMax = newDevice->joydev->axes[i].maximum;
457 default_axis_map[idx] = i;
458 idx++;
460 else
461 newDevice->dev_axes_to_di[i] = -1;
464 for (i = 0; i < WINE_JOYSTICK_MAX_POVS; i++)
466 if (test_bit(newDevice->joydev->absbits, ABS_HAT0X + i * 2) &&
467 test_bit(newDevice->joydev->absbits, ABS_HAT0Y + i * 2))
469 newDevice->generic.device_axis_count += 2;
470 newDevice->generic.props[idx ].lDevMin = newDevice->joydev->axes[ABS_HAT0X + i * 2].minimum;
471 newDevice->generic.props[idx ].lDevMax = newDevice->joydev->axes[ABS_HAT0X + i * 2].maximum;
472 newDevice->dev_axes_to_di[ABS_HAT0X + i * 2] = idx;
473 newDevice->generic.props[idx+1].lDevMin = newDevice->joydev->axes[ABS_HAT0Y + i * 2].minimum;
474 newDevice->generic.props[idx+1].lDevMax = newDevice->joydev->axes[ABS_HAT0Y + i * 2].maximum;
475 newDevice->dev_axes_to_di[ABS_HAT0Y + i * 2] = idx + 1;
477 default_axis_map[idx] = default_axis_map[idx + 1] = WINE_JOYSTICK_MAX_AXES + i;
478 idx += 2;
480 else
481 newDevice->dev_axes_to_di[ABS_HAT0X + i * 2] = newDevice->dev_axes_to_di[ABS_HAT0Y + i * 2] = -1;
484 /* do any user specified configuration */
485 if (setup_dinput_options(&newDevice->generic, default_axis_map) != DI_OK) goto failed;
487 /* Create copy of default data format */
488 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto failed;
489 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
490 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
493 /* Construct internal data format */
495 /* Supported Axis & POVs */
496 for (i = 0, idx = 0; i < newDevice->generic.device_axis_count; i++)
498 int wine_obj = newDevice->generic.axis_map[i];
500 if (wine_obj < 0) continue;
502 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
503 if (wine_obj < 8)
504 df->rgodf[idx].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
505 else
507 df->rgodf[idx].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
508 i++; /* POV takes 2 axes */
511 newDevice->generic.props[idx].lMin = 0;
512 newDevice->generic.props[idx].lMax = 0xffff;
513 newDevice->generic.props[idx].lSaturation = 0;
514 newDevice->generic.props[idx].lDeadZone = newDevice->generic.deadzone;
516 /* Linux supports force-feedback on X & Y axes only */
517 if (newDevice->joydev->has_ff && (i == 0 || i == 1))
518 df->rgodf[idx].dwFlags |= DIDOI_FFACTUATOR;
520 idx++;
523 /* Buttons can be anywhere, so check all */
524 for (i = 0; i < KEY_MAX && newDevice->generic.devcaps.dwButtons < WINE_JOYSTICK_MAX_BUTTONS; i++)
526 if (!test_bit(newDevice->joydev->keybits, i)) continue;
528 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[newDevice->generic.devcaps.dwButtons + 12], df->dwObjSize);
529 newDevice->buttons[i] = 0x80 | newDevice->generic.devcaps.dwButtons;
530 df->rgodf[idx ].pguid = &GUID_Button;
531 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->generic.devcaps.dwButtons++) | DIDFT_PSHBUTTON;
533 df->dwNumObjs = idx;
534 newDevice->generic.base.data_format.wine_df = df;
536 fake_current_js_state(newDevice);
538 /* Fill the caps */
539 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
540 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
541 if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
542 newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
543 else
544 newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
546 if (newDevice->joydev->has_ff)
547 newDevice->generic.devcaps.dwFlags |= DIDC_FORCEFEEDBACK;
549 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
551 EnterCriticalSection(&dinput->crit);
552 list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
553 LeaveCriticalSection(&dinput->crit);
555 return newDevice;
557 failed:
558 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
559 HeapFree(GetProcessHeap(), 0, df);
560 HeapFree(GetProcessHeap(), 0, newDevice->generic.axis_map);
561 HeapFree(GetProcessHeap(), 0, newDevice);
562 return NULL;
565 /******************************************************************************
566 * get_joystick_index : Get the joystick index from a given GUID
568 static unsigned short get_joystick_index(REFGUID guid)
570 GUID wine_joystick = DInput_Wine_Joystick_Base_GUID;
571 GUID dev_guid = *guid;
573 wine_joystick.Data3 = 0;
574 dev_guid.Data3 = 0;
576 /* for the standard joystick GUID use index 0 */
577 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
579 /* for the wine joystick GUIDs use the index stored in Data3 */
580 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3 - DInput_Wine_Joystick_Base_GUID.Data3;
582 return MAX_JOYDEV;
585 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
587 unsigned short index;
589 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
590 find_joydevs();
591 *pdev = NULL;
593 if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
594 have_joydevs && index < have_joydevs)
596 JoystickImpl *This;
598 if (riid == NULL)
599 ;/* nothing */
600 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
601 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
602 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
603 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
605 unicode = 0;
607 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
608 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
609 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
610 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
612 unicode = 1;
614 else
616 WARN("no interface\n");
617 return DIERR_NOINTERFACE;
620 This = alloc_device(rguid, dinput, index);
621 TRACE("Created a Joystick device (%p)\n", This);
623 if (!This) return DIERR_OUTOFMEMORY;
625 if (unicode)
626 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
627 else
628 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
630 return DI_OK;
633 return DIERR_DEVICENOTREG;
637 const struct dinput_device joystick_linuxinput_device = {
638 "Wine Linux-input joystick driver",
639 joydev_enum_deviceA,
640 joydev_enum_deviceW,
641 joydev_create_device
644 /******************************************************************************
645 * Acquire : gets exclusive control of the joystick
647 static HRESULT WINAPI JoystickWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
649 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
650 HRESULT res;
652 TRACE("(this=%p)\n",This);
654 if ((res = IDirectInputDevice2WImpl_Acquire(iface)) != DI_OK)
656 WARN("Failed to acquire: %x\n", res);
657 return res;
660 if ((This->joyfd = open(This->joydev->device, O_RDWR)) == -1)
662 if ((This->joyfd = open(This->joydev->device, O_RDONLY)) == -1)
664 /* Couldn't open the device at all */
665 ERR("Failed to open device %s: %d %s\n", This->joydev->device, errno, strerror(errno));
666 IDirectInputDevice2WImpl_Unacquire(iface);
667 return DIERR_NOTFOUND;
669 else
671 /* Couldn't open in r/w but opened in read-only. */
672 WARN("Could not open %s in read-write mode. Force feedback will be disabled.\n", This->joydev->device);
675 else
677 struct input_event event;
679 event.type = EV_FF;
680 event.code = FF_GAIN;
681 event.value = This->ff_gain;
682 if (write(This->joyfd, &event, sizeof(event)) == -1)
683 ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
684 if (!This->ff_autocenter)
686 /* Disable autocenter. */
687 event.code = FF_AUTOCENTER;
688 event.value = 0;
689 if (write(This->joyfd, &event, sizeof(event)) == -1)
690 ERR("Failed disabling autocenter: %d %s\n", errno, strerror(errno));
694 return DI_OK;
697 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
699 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
700 return JoystickWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
703 /******************************************************************************
704 * Unacquire : frees the joystick
706 static HRESULT WINAPI JoystickWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
708 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
709 HRESULT res;
711 TRACE("(this=%p)\n",This);
712 res = IDirectInputDevice2WImpl_Unacquire(iface);
713 if (res==DI_OK && This->joyfd!=-1) {
714 effect_list_item *itr;
715 struct input_event event;
717 /* For each known effect:
718 * - stop it
719 * - unload it
720 * But, unlike DISFFC_RESET, do not release the effect.
722 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry) {
723 IDirectInputEffect_Stop(itr->ref);
724 IDirectInputEffect_Unload(itr->ref);
727 /* Enable autocenter. */
728 event.type = EV_FF;
729 event.code = FF_AUTOCENTER;
730 /* TODO: Read autocenter strength before disabling it, and use it here
731 * instead of 0xFFFF (maximum strength).
733 event.value = 0xFFFF;
734 if (write(This->joyfd, &event, sizeof(event)) == -1)
735 ERR("Failed to set autocenter to %04x: %d %s\n", event.value, errno, strerror(errno));
737 close(This->joyfd);
738 This->joyfd = -1;
740 return res;
743 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
745 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
746 return JoystickWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
750 * set the current state of the js device as it would be with the middle
751 * values on the axes
753 #define CENTER_AXIS(a) \
754 (ji->dev_axes_to_di[a] == -1 ? 0 : joystick_map_axis( &ji->generic.props[ji->dev_axes_to_di[a]], \
755 ji->joydev->axes[a].value ))
756 static void fake_current_js_state(JoystickImpl *ji)
758 int i;
760 /* center the axes */
761 ji->generic.js.lX = CENTER_AXIS(ABS_X);
762 ji->generic.js.lY = CENTER_AXIS(ABS_Y);
763 ji->generic.js.lZ = CENTER_AXIS(ABS_Z);
764 ji->generic.js.lRx = CENTER_AXIS(ABS_RX);
765 ji->generic.js.lRy = CENTER_AXIS(ABS_RY);
766 ji->generic.js.lRz = CENTER_AXIS(ABS_RZ);
767 ji->generic.js.rglSlider[0] = CENTER_AXIS(ABS_THROTTLE);
768 ji->generic.js.rglSlider[1] = CENTER_AXIS(ABS_RUDDER);
770 /* POV center is -1 */
771 for (i = 0; i < 4; i++)
772 ji->generic.js.rgdwPOV[i] = -1;
774 #undef CENTER_AXIS
776 /* convert wine format offset to user format object index */
777 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
779 struct pollfd plfd;
780 struct input_event ie;
781 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
783 if (This->joyfd==-1)
784 return;
786 while (1)
788 LONG value = 0;
789 int inst_id = -1;
791 plfd.fd = This->joyfd;
792 plfd.events = POLLIN;
794 if (poll(&plfd,1,0) != 1)
795 return;
797 /* we have one event, so we can read */
798 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
799 return;
801 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
802 switch (ie.type) {
803 case EV_KEY: /* button */
805 int btn = This->buttons[ie.code];
807 TRACE("(%p) %d -> %d\n", This, ie.code, btn);
808 if (btn & 0x80)
810 btn &= 0x7F;
811 inst_id = DIDFT_MAKEINSTANCE(btn) | DIDFT_PSHBUTTON;
812 This->generic.js.rgbButtons[btn] = value = ie.value ? 0x80 : 0x00;
814 break;
816 case EV_ABS:
818 int axis = This->dev_axes_to_di[ie.code];
820 /* User axis remapping */
821 if (axis < 0) break;
822 axis = This->generic.axis_map[axis];
823 if (axis < 0) break;
825 inst_id = axis < 8 ? DIDFT_MAKEINSTANCE(axis) | DIDFT_ABSAXIS :
826 DIDFT_MAKEINSTANCE(axis - 8) | DIDFT_POV;
827 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], ie.value);
829 switch (axis) {
830 case 0: This->generic.js.lX = value; break;
831 case 1: This->generic.js.lY = value; break;
832 case 2: This->generic.js.lZ = value; break;
833 case 3: This->generic.js.lRx = value; break;
834 case 4: This->generic.js.lRy = value; break;
835 case 5: This->generic.js.lRz = value; break;
836 case 6: This->generic.js.rglSlider[0] = value; break;
837 case 7: This->generic.js.rglSlider[1] = value; break;
838 case 8: case 9: case 10: case 11:
840 int idx = axis - 8;
842 if (ie.code % 2)
843 This->povs[idx].y = ie.value;
844 else
845 This->povs[idx].x = ie.value;
847 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
848 break;
850 default:
851 FIXME("unhandled joystick axis event (code %d, value %d)\n",ie.code,ie.value);
853 break;
855 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
856 case EV_FF_STATUS:
857 This->ff_state = ie.value;
858 break;
859 #endif
860 #ifdef EV_SYN
861 case EV_SYN:
862 /* there is nothing to do */
863 break;
864 #endif
865 #ifdef EV_MSC
866 case EV_MSC:
867 /* Ignore */
868 break;
869 #endif
870 default:
871 FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
872 break;
874 if (inst_id >= 0)
875 queue_event(iface, inst_id,
876 value, GetCurrentTime(), This->generic.base.dinput->evsequence++);
880 /******************************************************************************
881 * SetProperty : change input device properties
883 static HRESULT WINAPI JoystickWImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
885 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
887 if (!ph) {
888 WARN("invalid argument\n");
889 return DIERR_INVALIDPARAM;
892 TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
893 TRACE("ph.dwSize = %d, ph.dwHeaderSize =%d, ph.dwObj = %d, ph.dwHow= %d\n",
894 ph->dwSize, ph->dwHeaderSize, ph->dwObj, ph->dwHow);
896 if (IS_DIPROP(rguid)) {
897 switch (LOWORD(rguid)) {
898 case (DWORD_PTR)DIPROP_CALIBRATIONMODE: {
899 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
900 FIXME("DIPROP_CALIBRATIONMODE(%d)\n", pd->dwData);
901 break;
903 case (DWORD_PTR)DIPROP_AUTOCENTER: {
904 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
906 TRACE("autocenter(%d)\n", pd->dwData);
907 This->ff_autocenter = pd->dwData == DIPROPAUTOCENTER_ON;
909 break;
911 case (DWORD_PTR)DIPROP_FFGAIN: {
912 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
914 TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
915 This->ff_gain = MulDiv(pd->dwData, 0xFFFF, 10000);
916 if (This->generic.base.acquired) {
917 /* Update immediately. */
918 struct input_event event;
920 event.type = EV_FF;
921 event.code = FF_GAIN;
922 event.value = This->ff_gain;
923 if (write(This->joyfd, &event, sizeof(event)) == -1)
924 ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
926 break;
928 default:
929 return JoystickWGenericImpl_SetProperty(iface, rguid, ph);
932 return DI_OK;
935 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
937 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
938 return JoystickWImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
941 /******************************************************************************
942 * GetProperty : get input device properties
944 static HRESULT WINAPI JoystickWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
946 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
948 TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
949 _dump_DIPROPHEADER(pdiph);
951 if (!IS_DIPROP(rguid)) return DI_OK;
953 switch (LOWORD(rguid)) {
954 case (DWORD_PTR) DIPROP_AUTOCENTER:
956 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
958 pd->dwData = This->ff_autocenter ? DIPROPAUTOCENTER_ON : DIPROPAUTOCENTER_OFF;
959 TRACE("autocenter(%d)\n", pd->dwData);
960 break;
962 case (DWORD_PTR) DIPROP_FFGAIN:
964 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
966 pd->dwData = MulDiv(This->ff_gain, 10000, 0xFFFF);
967 TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
968 break;
971 case (DWORD_PTR) DIPROP_VIDPID:
973 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
975 if (!This->joydev->product_id || !This->joydev->vendor_id)
976 return DIERR_UNSUPPORTED;
977 pd->dwData = MAKELONG(This->joydev->vendor_id, This->joydev->product_id);
978 TRACE("DIPROP_VIDPID(%08x)\n", pd->dwData);
979 break;
982 case (DWORD_PTR) DIPROP_JOYSTICKID:
984 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
986 pd->dwData = get_joystick_index(&This->generic.base.guid);
987 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
988 break;
991 default:
992 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
995 return DI_OK;
998 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
1000 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1001 return JoystickWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
1004 /******************************************************************************
1005 * CreateEffect - Create a new FF effect with the specified params
1007 static HRESULT WINAPI JoystickWImpl_CreateEffect(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid,
1008 LPCDIEFFECT lpeff, LPDIRECTINPUTEFFECT *ppdef,
1009 LPUNKNOWN pUnkOuter)
1011 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1012 effect_list_item* new_effect = NULL;
1013 HRESULT retval = DI_OK;
1014 #endif
1016 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1017 TRACE("(this=%p,%p,%p,%p,%p)\n", This, rguid, lpeff, ppdef, pUnkOuter);
1019 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
1020 TRACE("not available (compiled w/o ff support)\n");
1021 *ppdef = NULL;
1022 return DI_OK;
1023 #else
1025 if (!(new_effect = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_effect))))
1026 return DIERR_OUTOFMEMORY;
1028 retval = linuxinput_create_effect(&This->joyfd, rguid, &new_effect->entry, &new_effect->ref);
1029 if (retval != DI_OK)
1031 HeapFree(GetProcessHeap(), 0, new_effect);
1032 return retval;
1035 if (lpeff != NULL)
1037 retval = IDirectInputEffect_SetParameters(new_effect->ref, lpeff, 0);
1039 if (retval != DI_OK && retval != DI_DOWNLOADSKIPPED)
1041 HeapFree(GetProcessHeap(), 0, new_effect);
1042 return retval;
1046 list_add_tail(&This->ff_effects, &new_effect->entry);
1047 *ppdef = new_effect->ref;
1049 if (pUnkOuter != NULL)
1050 FIXME("Interface aggregation not implemented.\n");
1052 return DI_OK;
1054 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */
1057 static HRESULT WINAPI JoystickAImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid,
1058 LPCDIEFFECT lpeff, LPDIRECTINPUTEFFECT *ppdef,
1059 LPUNKNOWN pUnkOuter)
1061 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1062 return JoystickWImpl_CreateEffect(IDirectInputDevice8W_from_impl(This), rguid, lpeff, ppdef, pUnkOuter);
1065 /*******************************************************************************
1066 * EnumEffects - Enumerate available FF effects
1068 static HRESULT WINAPI JoystickAImpl_EnumEffects(LPDIRECTINPUTDEVICE8A iface,
1069 LPDIENUMEFFECTSCALLBACKA lpCallback,
1070 LPVOID pvRef,
1071 DWORD dwEffType)
1073 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1074 DIEFFECTINFOA dei; /* feif */
1075 DWORD type = DIEFT_GETTYPE(dwEffType);
1076 JoystickImpl* This = impl_from_IDirectInputDevice8A(iface);
1078 TRACE("(this=%p,%p,%d) type=%d\n", This, pvRef, dwEffType, type);
1080 dei.dwSize = sizeof(DIEFFECTINFOA);
1082 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1083 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1084 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1085 (*lpCallback)(&dei, pvRef);
1088 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1089 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1090 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1091 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1092 (*lpCallback)(&dei, pvRef);
1094 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1095 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1096 (*lpCallback)(&dei, pvRef);
1098 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1099 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1100 (*lpCallback)(&dei, pvRef);
1102 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1103 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1104 (*lpCallback)(&dei, pvRef);
1106 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1107 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1108 (*lpCallback)(&dei, pvRef);
1112 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1113 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1114 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1115 (*lpCallback)(&dei, pvRef);
1118 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1119 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1120 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1121 (*lpCallback)(&dei, pvRef);
1123 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1124 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1125 (*lpCallback)(&dei, pvRef);
1127 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1128 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1129 (*lpCallback)(&dei, pvRef);
1131 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1132 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1133 (*lpCallback)(&dei, pvRef);
1137 #endif
1139 return DI_OK;
1142 static HRESULT WINAPI JoystickWImpl_EnumEffects(LPDIRECTINPUTDEVICE8W iface,
1143 LPDIENUMEFFECTSCALLBACKW lpCallback,
1144 LPVOID pvRef,
1145 DWORD dwEffType)
1147 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1148 /* seems silly to duplicate all this code but all the structures and functions
1149 * are actually different (A/W) */
1150 DIEFFECTINFOW dei; /* feif */
1151 DWORD type = DIEFT_GETTYPE(dwEffType);
1152 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1153 int xfd = This->joyfd;
1155 TRACE("(this=%p,%p,%d) type=%d fd=%d\n", This, pvRef, dwEffType, type, xfd);
1157 dei.dwSize = sizeof(DIEFFECTINFOW);
1159 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1160 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1161 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1162 (*lpCallback)(&dei, pvRef);
1165 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1166 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1167 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1168 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1169 (*lpCallback)(&dei, pvRef);
1171 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1172 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1173 (*lpCallback)(&dei, pvRef);
1175 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1176 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1177 (*lpCallback)(&dei, pvRef);
1179 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1180 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1181 (*lpCallback)(&dei, pvRef);
1183 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1184 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1185 (*lpCallback)(&dei, pvRef);
1189 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1190 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1191 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1192 (*lpCallback)(&dei, pvRef);
1195 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1196 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1197 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1198 (*lpCallback)(&dei, pvRef);
1200 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1201 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1202 (*lpCallback)(&dei, pvRef);
1204 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1205 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1206 (*lpCallback)(&dei, pvRef);
1208 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1209 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1210 (*lpCallback)(&dei, pvRef);
1214 /* return to unacquired state if that's where it was */
1215 if (xfd == -1)
1216 IDirectInputDevice8_Unacquire(iface);
1217 #endif
1219 return DI_OK;
1222 /*******************************************************************************
1223 * GetEffectInfo - Get information about a particular effect
1225 static HRESULT WINAPI JoystickAImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8A iface,
1226 LPDIEFFECTINFOA pdei,
1227 REFGUID guid)
1229 JoystickImpl* This = impl_from_IDirectInputDevice8A(iface);
1231 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1233 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1234 return linuxinput_get_info_A(This->joyfd, guid, pdei);
1235 #else
1236 return DI_OK;
1237 #endif
1240 static HRESULT WINAPI JoystickWImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8W iface,
1241 LPDIEFFECTINFOW pdei,
1242 REFGUID guid)
1244 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1246 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1248 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1249 return linuxinput_get_info_W(This->joyfd, guid, pdei);
1250 #else
1251 return DI_OK;
1252 #endif
1255 /*******************************************************************************
1256 * GetForceFeedbackState - Get information about the device's FF state
1258 static HRESULT WINAPI JoystickWImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8W iface, LPDWORD pdwOut)
1260 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1262 TRACE("(this=%p,%p)\n", This, pdwOut);
1264 (*pdwOut) = 0;
1266 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1267 /* DIGFFS_STOPPED is the only mandatory flag to report */
1268 if (This->ff_state == FF_STATUS_STOPPED)
1269 (*pdwOut) |= DIGFFS_STOPPED;
1270 #endif
1272 return DI_OK;
1275 static HRESULT WINAPI JoystickAImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8A iface, LPDWORD pdwOut)
1277 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1278 return JoystickWImpl_GetForceFeedbackState(IDirectInputDevice8W_from_impl(This), pdwOut);
1281 /*******************************************************************************
1282 * SendForceFeedbackCommand - Send a command to the device's FF system
1284 static HRESULT WINAPI JoystickWImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8W iface, DWORD dwFlags)
1286 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1287 TRACE("(this=%p,%d)\n", This, dwFlags);
1289 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1290 switch (dwFlags)
1292 case DISFFC_STOPALL:
1294 /* Stop all effects */
1295 effect_list_item *itr;
1297 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry)
1298 IDirectInputEffect_Stop(itr->ref);
1299 break;
1302 case DISFFC_RESET:
1304 effect_list_item *itr, *ptr;
1306 /* Stop, unload, release and free all effects */
1307 /* This returns the device to its "bare" state */
1308 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1309 IDirectInputEffect_Release(itr->ref);
1310 break;
1312 case DISFFC_PAUSE:
1313 case DISFFC_CONTINUE:
1314 FIXME("No support for Pause or Continue in linux\n");
1315 break;
1317 case DISFFC_SETACTUATORSOFF:
1318 case DISFFC_SETACTUATORSON:
1319 FIXME("No direct actuator control in linux\n");
1320 break;
1322 default:
1323 FIXME("Unknown Force Feedback Command!\n");
1324 return DIERR_INVALIDPARAM;
1326 return DI_OK;
1327 #else
1328 return DIERR_UNSUPPORTED;
1329 #endif
1332 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8A iface, DWORD dwFlags)
1334 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1335 return JoystickWImpl_SendForceFeedbackCommand(IDirectInputDevice8W_from_impl(This), dwFlags);
1338 /*******************************************************************************
1339 * EnumCreatedEffectObjects - Enumerate all the effects that have been
1340 * created for this device.
1342 static HRESULT WINAPI JoystickWImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8W iface,
1343 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1344 LPVOID pvRef, DWORD dwFlags)
1346 /* this function is safe to call on non-ff-enabled builds */
1347 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1348 effect_list_item *itr, *ptr;
1350 TRACE("(this=%p,%p,%p,%d)\n", This, lpCallback, pvRef, dwFlags);
1352 if (!lpCallback)
1353 return DIERR_INVALIDPARAM;
1355 if (dwFlags != 0)
1356 FIXME("Flags specified, but no flags exist yet (DX9)!\n");
1358 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1359 (*lpCallback)(itr->ref, pvRef);
1361 return DI_OK;
1364 static HRESULT WINAPI JoystickAImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8A iface,
1365 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1366 LPVOID pvRef, DWORD dwFlags)
1368 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1369 return JoystickWImpl_EnumCreatedEffectObjects(IDirectInputDevice8W_from_impl(This), lpCallback, pvRef, dwFlags);
1372 /******************************************************************************
1373 * GetDeviceInfo : get information about a device's identity
1375 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface,
1376 LPDIDEVICEINSTANCEA pdidi)
1378 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1380 TRACE("(%p) %p\n", This, pdidi);
1382 if (pdidi == NULL) return E_POINTER;
1383 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1384 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)))
1385 return DIERR_INVALIDPARAM;
1387 fill_joystick_dideviceinstanceA(pdidi, This->generic.base.dinput->dwVersion,
1388 get_joystick_index(&This->generic.base.guid));
1389 return DI_OK;
1392 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface,
1393 LPDIDEVICEINSTANCEW pdidi)
1395 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
1397 TRACE("(%p) %p\n", This, pdidi);
1399 if (pdidi == NULL) return E_POINTER;
1400 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1401 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)))
1402 return DIERR_INVALIDPARAM;
1404 fill_joystick_dideviceinstanceW(pdidi, This->generic.base.dinput->dwVersion,
1405 get_joystick_index(&This->generic.base.guid));
1406 return DI_OK;
1409 static const IDirectInputDevice8AVtbl JoystickAvt =
1411 IDirectInputDevice2AImpl_QueryInterface,
1412 IDirectInputDevice2AImpl_AddRef,
1413 IDirectInputDevice2AImpl_Release,
1414 JoystickAGenericImpl_GetCapabilities,
1415 IDirectInputDevice2AImpl_EnumObjects,
1416 JoystickAImpl_GetProperty,
1417 JoystickAImpl_SetProperty,
1418 JoystickAImpl_Acquire,
1419 JoystickAImpl_Unacquire,
1420 JoystickAGenericImpl_GetDeviceState,
1421 IDirectInputDevice2AImpl_GetDeviceData,
1422 IDirectInputDevice2AImpl_SetDataFormat,
1423 IDirectInputDevice2AImpl_SetEventNotification,
1424 IDirectInputDevice2AImpl_SetCooperativeLevel,
1425 JoystickAGenericImpl_GetObjectInfo,
1426 JoystickAImpl_GetDeviceInfo,
1427 IDirectInputDevice2AImpl_RunControlPanel,
1428 IDirectInputDevice2AImpl_Initialize,
1429 JoystickAImpl_CreateEffect,
1430 JoystickAImpl_EnumEffects,
1431 JoystickAImpl_GetEffectInfo,
1432 JoystickAImpl_GetForceFeedbackState,
1433 JoystickAImpl_SendForceFeedbackCommand,
1434 JoystickAImpl_EnumCreatedEffectObjects,
1435 IDirectInputDevice2AImpl_Escape,
1436 JoystickAGenericImpl_Poll,
1437 IDirectInputDevice2AImpl_SendDeviceData,
1438 IDirectInputDevice7AImpl_EnumEffectsInFile,
1439 IDirectInputDevice7AImpl_WriteEffectToFile,
1440 JoystickAGenericImpl_BuildActionMap,
1441 JoystickAGenericImpl_SetActionMap,
1442 IDirectInputDevice8AImpl_GetImageInfo
1445 static const IDirectInputDevice8WVtbl JoystickWvt =
1447 IDirectInputDevice2WImpl_QueryInterface,
1448 IDirectInputDevice2WImpl_AddRef,
1449 IDirectInputDevice2WImpl_Release,
1450 JoystickWGenericImpl_GetCapabilities,
1451 IDirectInputDevice2WImpl_EnumObjects,
1452 JoystickWImpl_GetProperty,
1453 JoystickWImpl_SetProperty,
1454 JoystickWImpl_Acquire,
1455 JoystickWImpl_Unacquire,
1456 JoystickWGenericImpl_GetDeviceState,
1457 IDirectInputDevice2WImpl_GetDeviceData,
1458 IDirectInputDevice2WImpl_SetDataFormat,
1459 IDirectInputDevice2WImpl_SetEventNotification,
1460 IDirectInputDevice2WImpl_SetCooperativeLevel,
1461 JoystickWGenericImpl_GetObjectInfo,
1462 JoystickWImpl_GetDeviceInfo,
1463 IDirectInputDevice2WImpl_RunControlPanel,
1464 IDirectInputDevice2WImpl_Initialize,
1465 JoystickWImpl_CreateEffect,
1466 JoystickWImpl_EnumEffects,
1467 JoystickWImpl_GetEffectInfo,
1468 JoystickWImpl_GetForceFeedbackState,
1469 JoystickWImpl_SendForceFeedbackCommand,
1470 JoystickWImpl_EnumCreatedEffectObjects,
1471 IDirectInputDevice2WImpl_Escape,
1472 JoystickWGenericImpl_Poll,
1473 IDirectInputDevice2WImpl_SendDeviceData,
1474 IDirectInputDevice7WImpl_EnumEffectsInFile,
1475 IDirectInputDevice7WImpl_WriteEffectToFile,
1476 JoystickWGenericImpl_BuildActionMap,
1477 JoystickWGenericImpl_SetActionMap,
1478 IDirectInputDevice8WImpl_GetImageInfo
1481 #else /* HAS_PROPER_HEADER */
1483 const struct dinput_device joystick_linuxinput_device = {
1484 "Wine Linux-input joystick driver",
1485 NULL,
1486 NULL,
1487 NULL
1490 #endif /* HAS_PROPER_HEADER */