ieframe: Fix memory leak in PersistFile_Save.
[wine.git] / dlls / dinput / joystick_linuxinput.c
blob102de8269b9ca1eaa847165f70438befb173fa38
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 "devguid.h"
61 #include "dinput.h"
63 #include "dinput_private.h"
64 #include "device_private.h"
65 #include "joystick_private.h"
67 #ifdef HAS_PROPER_HEADER
69 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
71 #define EVDEVPREFIX "/dev/input/event"
72 #define EVDEVDRIVER " (event)"
74 /* Wine joystick driver object instances */
75 #define WINE_JOYSTICK_MAX_AXES 8
76 #define WINE_JOYSTICK_MAX_POVS 4
77 #define WINE_JOYSTICK_MAX_BUTTONS 128
79 struct wine_input_absinfo {
80 LONG value;
81 LONG minimum;
82 LONG maximum;
83 LONG fuzz;
84 LONG flat;
87 /* implemented in effect_linuxinput.c */
88 HRESULT linuxinput_create_effect(int* fd, REFGUID rguid, struct list *parent_list_entry, LPDIRECTINPUTEFFECT* peff);
89 HRESULT linuxinput_get_info_A(int fd, REFGUID rguid, LPDIEFFECTINFOA info);
90 HRESULT linuxinput_get_info_W(int fd, REFGUID rguid, LPDIEFFECTINFOW info);
92 static HRESULT WINAPI JoystickWImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8W iface, DWORD dwFlags);
94 typedef struct JoystickImpl JoystickImpl;
95 static const IDirectInputDevice8AVtbl JoystickAvt;
96 static const IDirectInputDevice8WVtbl JoystickWvt;
98 struct JoyDev {
99 char *device;
100 char *name;
101 GUID guid;
102 GUID guid_product;
104 BOOL has_ff, is_joystick;
105 int num_effects;
107 /* data returned by EVIOCGBIT for caps, EV_ABS, EV_KEY, and EV_FF */
108 BYTE evbits[(EV_MAX+7)/8];
109 BYTE absbits[(ABS_MAX+7)/8];
110 BYTE keybits[(KEY_MAX+7)/8];
111 BYTE ffbits[(FF_MAX+7)/8];
113 /* data returned by the EVIOCGABS() ioctl */
114 struct wine_input_absinfo axes[ABS_MAX];
116 WORD vendor_id, product_id, bus_type;
119 struct JoystickImpl
121 struct JoystickGenericImpl generic;
122 struct JoyDev *joydev;
124 /* joystick private */
125 int joyfd;
127 int dev_axes_to_di[ABS_MAX];
128 POINTL povs[4];
130 /* LUT for KEY_ to offset in rgbButtons */
131 BYTE buttons[KEY_MAX];
133 /* Force feedback variables */
134 struct list ff_effects;
135 int ff_state;
136 int ff_autocenter;
137 int ff_gain;
140 static inline JoystickImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
142 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface),
143 JoystickGenericImpl, base), JoystickImpl, generic);
145 static inline JoystickImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
147 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface),
148 JoystickGenericImpl, base), JoystickImpl, generic);
151 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickImpl *This)
153 return &This->generic.base.IDirectInputDevice8W_iface;
156 static void fake_current_js_state(JoystickImpl *ji);
157 static void find_joydevs(void);
158 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface);
160 /* This GUID is slightly different from the linux joystick one. Take note. */
161 static const GUID DInput_Wine_Joystick_Base_GUID = { /* 9e573eda-7734-11d2-8d4a-23903fb6bdf7 */
162 0x9e573eda,
163 0x7734,
164 0x11d2,
165 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
168 #define test_bit(arr,bit) (((BYTE*)(arr))[(bit)>>3]&(1<<((bit)&7)))
170 #define MAX_JOYDEV 64
172 static int have_joydevs = -1;
173 static struct JoyDev *joydevs = NULL;
175 static void find_joydevs(void)
177 int i;
179 if (InterlockedCompareExchange(&have_joydevs, 0, -1) != -1)
180 /* Someone beat us to it */
181 return;
183 for (i = 0; i < MAX_JOYDEV; i++)
185 char buf[MAX_PATH];
186 struct JoyDev joydev = {0};
187 int fd;
188 BOOL no_ff_check = FALSE;
189 int j;
190 struct JoyDev *new_joydevs;
191 struct input_id device_id = {0};
193 snprintf(buf, sizeof(buf), EVDEVPREFIX"%d", i);
195 if ((fd = open(buf, O_RDWR)) == -1)
197 fd = open(buf, O_RDONLY);
198 no_ff_check = TRUE;
201 if (fd == -1)
202 continue;
204 if (ioctl(fd, EVIOCGBIT(0, sizeof(joydev.evbits)), joydev.evbits) == -1)
206 WARN("ioctl(EVIOCGBIT, 0) failed: %d %s\n", errno, strerror(errno));
207 close(fd);
208 continue;
210 if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(joydev.absbits)), joydev.absbits) == -1)
212 WARN("ioctl(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno, strerror(errno));
213 close(fd);
214 continue;
216 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(joydev.keybits)), joydev.keybits) == -1)
218 WARN("ioctl(EVIOCGBIT, EV_KEY) failed: %d %s\n", errno, strerror(errno));
219 close(fd);
220 continue;
223 /* A true joystick has at least axis X and Y, and at least 1
224 * button. copied from linux/drivers/input/joydev.c */
225 if (((!test_bit(joydev.absbits, ABS_X) || !test_bit(joydev.absbits, ABS_Y)) &&
226 !test_bit(joydev.absbits, ABS_WHEEL) &&
227 !test_bit(joydev.absbits, ABS_GAS) &&
228 !test_bit(joydev.absbits, ABS_BRAKE)) ||
229 !(test_bit(joydev.keybits, BTN_TRIGGER) ||
230 test_bit(joydev.keybits, BTN_A) ||
231 test_bit(joydev.keybits, BTN_1) ||
232 test_bit(joydev.keybits, BTN_BASE) ||
233 test_bit(joydev.keybits, BTN_GEAR_UP) ||
234 test_bit(joydev.keybits, BTN_GEAR_DOWN)))
236 close(fd);
237 continue;
240 /* in lieu of properly reporting HID usage, detect presence of
241 * "joystick buttons" and report those devices as joysticks instead of
242 * gamepads */
243 joydev.is_joystick =
244 test_bit(joydev.keybits, BTN_TRIGGER) ||
245 test_bit(joydev.keybits, BTN_THUMB) ||
246 test_bit(joydev.keybits, BTN_THUMB2) ||
247 test_bit(joydev.keybits, BTN_TOP) ||
248 test_bit(joydev.keybits, BTN_TOP2) ||
249 test_bit(joydev.keybits, BTN_PINKIE) ||
250 test_bit(joydev.keybits, BTN_BASE) ||
251 test_bit(joydev.keybits, BTN_BASE2) ||
252 test_bit(joydev.keybits, BTN_BASE3) ||
253 test_bit(joydev.keybits, BTN_BASE4) ||
254 test_bit(joydev.keybits, BTN_BASE5) ||
255 test_bit(joydev.keybits, BTN_BASE6) ||
256 test_bit(joydev.keybits, BTN_GEAR_UP) ||
257 test_bit(joydev.keybits, BTN_GEAR_DOWN) ||
258 test_bit(joydev.keybits, BTN_DEAD);
260 if (!(joydev.device = HeapAlloc(GetProcessHeap(), 0, strlen(buf) + 1)))
262 close(fd);
263 continue;
265 strcpy(joydev.device, buf);
267 buf[MAX_PATH - 1] = 0;
268 if (ioctl(fd, EVIOCGNAME(MAX_PATH - 1), buf) != -1 &&
269 (joydev.name = HeapAlloc(GetProcessHeap(), 0, strlen(buf) + strlen(EVDEVDRIVER) + 1)))
271 strcpy(joydev.name, buf);
272 /* Append driver name */
273 strcat(joydev.name, EVDEVDRIVER);
275 else
276 joydev.name = joydev.device;
278 if (device_disabled_registry(joydev.name)) {
279 close(fd);
280 HeapFree(GetProcessHeap(), 0, joydev.name);
281 if (joydev.name != joydev.device)
282 HeapFree(GetProcessHeap(), 0, joydev.device);
283 continue;
286 joydev.guid = DInput_Wine_Joystick_Base_GUID;
287 joydev.guid.Data3 += have_joydevs;
289 TRACE("Found a joystick on %s: %s (%s)\n",
290 joydev.device, joydev.name,
291 debugstr_guid(&joydev.guid)
294 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
295 if (!no_ff_check &&
296 test_bit(joydev.evbits, EV_FF) &&
297 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(joydev.ffbits)), joydev.ffbits) != -1 &&
298 ioctl(fd, EVIOCGEFFECTS, &joydev.num_effects) != -1 &&
299 joydev.num_effects > 0)
301 TRACE(" ... with force feedback\n");
302 joydev.has_ff = TRUE;
304 #endif
306 for (j = 0; j < ABS_MAX;j ++)
308 if (!test_bit(joydev.absbits, j)) continue;
309 if (ioctl(fd, EVIOCGABS(j), &(joydev.axes[j])) != -1)
311 TRACE(" ... with axis %d: cur=%d, min=%d, max=%d, fuzz=%d, flat=%d\n",
313 joydev.axes[j].value,
314 joydev.axes[j].minimum,
315 joydev.axes[j].maximum,
316 joydev.axes[j].fuzz,
317 joydev.axes[j].flat
322 if (ioctl(fd, EVIOCGID, &device_id) == -1)
324 WARN("ioctl(EVIOCGID) failed: %d %s\n", errno, strerror(errno));
325 joydev.guid_product = DInput_Wine_Joystick_Base_GUID;
327 else
329 joydev.vendor_id = device_id.vendor;
330 joydev.product_id = device_id.product;
331 joydev.bus_type = device_id.bustype;
333 /* Concatenate product_id with vendor_id to mimic Windows behaviour */
334 joydev.guid_product = DInput_PIDVID_Product_GUID;
335 joydev.guid_product.Data1 = MAKELONG(joydev.vendor_id, joydev.product_id);
338 if (!have_joydevs)
339 new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
340 else
341 new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joydevs, (1 + have_joydevs) * sizeof(struct JoyDev));
343 if (!new_joydevs)
345 close(fd);
346 continue;
348 joydevs = new_joydevs;
349 joydevs[have_joydevs] = joydev;
350 have_joydevs++;
352 close(fd);
356 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
358 DWORD dwSize = lpddi->dwSize;
360 TRACE("%d %p\n", dwSize, lpddi);
361 memset(lpddi, 0, dwSize);
363 lpddi->dwSize = dwSize;
364 lpddi->guidInstance = joydevs[id].guid;
365 lpddi->guidProduct = joydevs[id].guid_product;
366 lpddi->guidFFDriver = GUID_NULL;
367 lpddi->dwDevType = get_device_type(version, joydevs[id].is_joystick);
369 /* Assume the joystick as HID if it is attached to USB bus and has a valid VID/PID */
370 if (joydevs[id].bus_type == BUS_USB &&
371 joydevs[id].vendor_id && joydevs[id].product_id)
373 lpddi->dwDevType |= DIDEVTYPE_HID;
374 lpddi->wUsagePage = 0x01; /* Desktop */
375 if (joydevs[id].is_joystick)
376 lpddi->wUsage = 0x04; /* Joystick */
377 else
378 lpddi->wUsage = 0x05; /* Game Pad */
381 MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
382 MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszProductName, MAX_PATH);
385 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
387 DIDEVICEINSTANCEW lpddiW;
388 DWORD dwSize = lpddi->dwSize;
390 lpddiW.dwSize = sizeof(lpddiW);
391 fill_joystick_dideviceinstanceW(&lpddiW, version, id);
393 TRACE("%d %p\n", dwSize, lpddi);
394 memset(lpddi, 0, dwSize);
396 /* Convert W->A */
397 lpddi->dwSize = dwSize;
398 lpddi->guidInstance = lpddiW.guidInstance;
399 lpddi->guidProduct = lpddiW.guidProduct;
400 lpddi->dwDevType = lpddiW.dwDevType;
401 lstrcpynA(lpddi->tszInstanceName, joydevs[id].name, MAX_PATH);
402 lstrcpynA(lpddi->tszProductName, joydevs[id].name, MAX_PATH);
403 lpddi->guidFFDriver = lpddiW.guidFFDriver;
404 lpddi->wUsagePage = lpddiW.wUsagePage;
405 lpddi->wUsage = lpddiW.wUsage;
408 static HRESULT joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
410 find_joydevs();
412 if (id >= have_joydevs) {
413 return E_FAIL;
416 if (!((dwDevType == 0) ||
417 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version >= 0x0300 && version < 0x0800)) ||
418 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
419 return S_FALSE;
421 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
422 if (dwFlags & DIEDFL_FORCEFEEDBACK)
423 return S_FALSE;
424 #endif
426 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
427 fill_joystick_dideviceinstanceA(lpddi, version, id);
428 return S_OK;
430 return S_FALSE;
433 static HRESULT joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
435 find_joydevs();
437 if (id >= have_joydevs) {
438 return E_FAIL;
441 if (!((dwDevType == 0) ||
442 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version >= 0x0300 && version < 0x0800)) ||
443 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
444 return S_FALSE;
446 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
447 if (dwFlags & DIEDFL_FORCEFEEDBACK)
448 return S_FALSE;
449 #endif
451 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
452 fill_joystick_dideviceinstanceW(lpddi, version, id);
453 return S_OK;
455 return S_FALSE;
458 static JoystickImpl *alloc_device(REFGUID rguid, IDirectInputImpl *dinput, unsigned short index)
460 JoystickImpl* newDevice;
461 LPDIDATAFORMAT df = NULL;
462 int i, idx = 0;
463 int default_axis_map[WINE_JOYSTICK_MAX_AXES + WINE_JOYSTICK_MAX_POVS*2];
464 DIDEVICEINSTANCEW ddi;
466 newDevice = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(JoystickImpl));
467 if (!newDevice) return NULL;
469 newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
470 newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
471 newDevice->generic.base.ref = 1;
472 newDevice->generic.base.guid = *rguid;
473 newDevice->generic.base.dinput = dinput;
474 newDevice->generic.joy_polldev = joy_polldev;
475 newDevice->joyfd = -1;
476 newDevice->joydev = &joydevs[index];
477 newDevice->generic.name = newDevice->joydev->name;
478 list_init(&newDevice->ff_effects);
479 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
480 newDevice->ff_state = FF_STATUS_STOPPED;
481 #endif
482 /* There is no way in linux to query force feedback autocenter status.
483 Instead, track it with ff_autocenter, and assume it's initially
484 enabled. */
485 newDevice->ff_autocenter = 1;
486 newDevice->ff_gain = 0xFFFF;
487 InitializeCriticalSection(&newDevice->generic.base.crit);
488 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
490 /* Count number of available axes - supported Axis & POVs */
491 for (i = 0; i < ABS_MAX; i++)
493 if (idx < WINE_JOYSTICK_MAX_AXES &&
494 i < ABS_HAT0X &&
495 test_bit(newDevice->joydev->absbits, i))
497 newDevice->generic.device_axis_count++;
498 newDevice->dev_axes_to_di[i] = idx;
499 newDevice->generic.props[idx].lDevMin = newDevice->joydev->axes[i].minimum;
500 newDevice->generic.props[idx].lDevMax = newDevice->joydev->axes[i].maximum;
501 if (i >= 8 && i <= 10) /* If it's a wheel axis... */
502 default_axis_map[idx] = i - 8; /* ... remap to X/Y/Z */
503 else
504 default_axis_map[idx] = i;
505 idx++;
507 else
508 newDevice->dev_axes_to_di[i] = -1;
511 for (i = 0; i < WINE_JOYSTICK_MAX_POVS; i++)
513 if (test_bit(newDevice->joydev->absbits, ABS_HAT0X + i * 2) &&
514 test_bit(newDevice->joydev->absbits, ABS_HAT0Y + i * 2))
516 newDevice->generic.device_axis_count += 2;
517 newDevice->generic.props[idx ].lDevMin = newDevice->joydev->axes[ABS_HAT0X + i * 2].minimum;
518 newDevice->generic.props[idx ].lDevMax = newDevice->joydev->axes[ABS_HAT0X + i * 2].maximum;
519 newDevice->dev_axes_to_di[ABS_HAT0X + i * 2] = idx;
520 newDevice->generic.props[idx+1].lDevMin = newDevice->joydev->axes[ABS_HAT0Y + i * 2].minimum;
521 newDevice->generic.props[idx+1].lDevMax = newDevice->joydev->axes[ABS_HAT0Y + i * 2].maximum;
522 newDevice->dev_axes_to_di[ABS_HAT0Y + i * 2] = idx + 1;
524 default_axis_map[idx] = default_axis_map[idx + 1] = WINE_JOYSTICK_MAX_AXES + i;
525 idx += 2;
527 else
528 newDevice->dev_axes_to_di[ABS_HAT0X + i * 2] = newDevice->dev_axes_to_di[ABS_HAT0Y + i * 2] = -1;
531 /* do any user specified configuration */
532 if (setup_dinput_options(&newDevice->generic, default_axis_map) != DI_OK) goto failed;
534 /* Create copy of default data format */
535 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto failed;
536 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
537 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
540 /* Construct internal data format */
542 /* Supported Axis & POVs */
543 for (i = 0, idx = 0; i < newDevice->generic.device_axis_count; i++)
545 int wine_obj = newDevice->generic.axis_map[i];
547 if (wine_obj < 0) continue;
549 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
550 if (wine_obj < 8)
551 df->rgodf[idx].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
552 else
554 df->rgodf[idx].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
555 i++; /* POV takes 2 axes */
558 newDevice->generic.props[idx].lMin = 0;
559 newDevice->generic.props[idx].lMax = 0xffff;
560 newDevice->generic.props[idx].lSaturation = 0;
561 newDevice->generic.props[idx].lDeadZone = newDevice->generic.deadzone;
563 /* Linux supports force-feedback on X & Y axes only */
564 if (newDevice->joydev->has_ff && (i == 0 || i == 1))
565 df->rgodf[idx].dwFlags |= DIDOI_FFACTUATOR;
567 idx++;
570 /* Buttons can be anywhere, so check all */
571 for (i = 0; i < KEY_MAX && newDevice->generic.devcaps.dwButtons < WINE_JOYSTICK_MAX_BUTTONS; i++)
573 if (!test_bit(newDevice->joydev->keybits, i)) continue;
575 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[newDevice->generic.devcaps.dwButtons + 12], df->dwObjSize);
576 newDevice->buttons[i] = 0x80 | newDevice->generic.devcaps.dwButtons;
577 df->rgodf[idx ].pguid = &GUID_Button;
578 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->generic.devcaps.dwButtons++) | DIDFT_PSHBUTTON;
580 df->dwNumObjs = idx;
581 newDevice->generic.base.data_format.wine_df = df;
583 fake_current_js_state(newDevice);
585 /* Fill the caps */
586 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
587 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
589 ddi.dwSize = sizeof(ddi);
590 fill_joystick_dideviceinstanceW(&ddi, newDevice->generic.base.dinput->dwVersion, index);
591 newDevice->generic.devcaps.dwDevType = ddi.dwDevType;
593 if (newDevice->joydev->has_ff)
594 newDevice->generic.devcaps.dwFlags |= DIDC_FORCEFEEDBACK;
596 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
598 return newDevice;
600 failed:
601 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
602 HeapFree(GetProcessHeap(), 0, df);
603 HeapFree(GetProcessHeap(), 0, newDevice->generic.axis_map);
604 HeapFree(GetProcessHeap(), 0, newDevice);
605 return NULL;
608 /******************************************************************************
609 * get_joystick_index : Get the joystick index from a given GUID
611 static unsigned short get_joystick_index(REFGUID guid)
613 GUID wine_joystick = DInput_Wine_Joystick_Base_GUID;
614 GUID dev_guid = *guid;
616 wine_joystick.Data3 = 0;
617 dev_guid.Data3 = 0;
619 /* for the standard joystick GUID use index 0 */
620 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
622 /* for the wine joystick GUIDs use the index stored in Data3 */
623 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3 - DInput_Wine_Joystick_Base_GUID.Data3;
625 return MAX_JOYDEV;
628 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
630 unsigned short index;
632 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
633 find_joydevs();
634 *pdev = NULL;
636 if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
637 have_joydevs && index < have_joydevs)
639 JoystickImpl *This;
641 if (riid == NULL)
642 ;/* nothing */
643 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
644 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
645 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
646 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
648 unicode = 0;
650 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
651 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
652 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
653 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
655 unicode = 1;
657 else
659 WARN("no interface\n");
660 return DIERR_NOINTERFACE;
663 This = alloc_device(rguid, dinput, index);
664 TRACE("Created a Joystick device (%p)\n", This);
666 if (!This) return DIERR_OUTOFMEMORY;
668 if (unicode)
669 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
670 else
671 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
673 return DI_OK;
676 return DIERR_DEVICENOTREG;
680 const struct dinput_device joystick_linuxinput_device = {
681 "Wine Linux-input joystick driver",
682 joydev_enum_deviceA,
683 joydev_enum_deviceW,
684 joydev_create_device
687 /******************************************************************************
688 * Acquire : gets exclusive control of the joystick
690 static HRESULT WINAPI JoystickWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
692 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
693 HRESULT res;
695 TRACE("(this=%p)\n",This);
697 if ((res = IDirectInputDevice2WImpl_Acquire(iface)) != DI_OK)
699 WARN("Failed to acquire: %x\n", res);
700 return res;
703 if ((This->joyfd = open(This->joydev->device, O_RDWR)) == -1)
705 if ((This->joyfd = open(This->joydev->device, O_RDONLY)) == -1)
707 /* Couldn't open the device at all */
708 ERR("Failed to open device %s: %d %s\n", This->joydev->device, errno, strerror(errno));
709 IDirectInputDevice2WImpl_Unacquire(iface);
710 return DIERR_NOTFOUND;
712 else
714 /* Couldn't open in r/w but opened in read-only. */
715 WARN("Could not open %s in read-write mode. Force feedback will be disabled.\n", This->joydev->device);
718 else
720 struct input_event event;
722 event.type = EV_FF;
723 event.code = FF_GAIN;
724 event.value = This->ff_gain;
725 if (write(This->joyfd, &event, sizeof(event)) == -1)
726 ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
727 if (!This->ff_autocenter)
729 /* Disable autocenter. */
730 event.code = FF_AUTOCENTER;
731 event.value = 0;
732 if (write(This->joyfd, &event, sizeof(event)) == -1)
733 ERR("Failed disabling autocenter: %d %s\n", errno, strerror(errno));
737 return DI_OK;
740 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
742 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
743 return JoystickWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
746 /******************************************************************************
747 * Unacquire : frees the joystick
749 static HRESULT WINAPI JoystickWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
751 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
752 HRESULT res;
754 TRACE("(this=%p)\n",This);
755 res = IDirectInputDevice2WImpl_Unacquire(iface);
756 if (res==DI_OK && This->joyfd!=-1) {
757 struct input_event event;
759 /* Stop and unload all effects */
760 JoystickWImpl_SendForceFeedbackCommand(iface, DISFFC_RESET);
762 /* Enable autocenter. */
763 event.type = EV_FF;
764 event.code = FF_AUTOCENTER;
765 /* TODO: Read autocenter strength before disabling it, and use it here
766 * instead of 0xFFFF (maximum strength).
768 event.value = 0xFFFF;
769 if (write(This->joyfd, &event, sizeof(event)) == -1)
770 ERR("Failed to set autocenter to %04x: %d %s\n", event.value, errno, strerror(errno));
772 close(This->joyfd);
773 This->joyfd = -1;
775 return res;
778 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
780 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
781 return JoystickWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
785 * set the current state of the js device as it would be with the middle
786 * values on the axes
788 #define CENTER_AXIS(a) \
789 (ji->dev_axes_to_di[a] == -1 ? 0 : joystick_map_axis( &ji->generic.props[ji->dev_axes_to_di[a]], \
790 ji->joydev->axes[a].value ))
791 static void fake_current_js_state(JoystickImpl *ji)
793 int i;
795 /* center the axes */
796 ji->generic.js.lX = CENTER_AXIS(ABS_X);
797 ji->generic.js.lY = CENTER_AXIS(ABS_Y);
798 ji->generic.js.lZ = CENTER_AXIS(ABS_Z);
799 ji->generic.js.lRx = CENTER_AXIS(ABS_RX);
800 ji->generic.js.lRy = CENTER_AXIS(ABS_RY);
801 ji->generic.js.lRz = CENTER_AXIS(ABS_RZ);
802 ji->generic.js.rglSlider[0] = CENTER_AXIS(ABS_THROTTLE);
803 ji->generic.js.rglSlider[1] = CENTER_AXIS(ABS_RUDDER);
805 /* POV center is -1 */
806 for (i = 0; i < 4; i++)
807 ji->generic.js.rgdwPOV[i] = -1;
809 #undef CENTER_AXIS
811 /* convert wine format offset to user format object index */
812 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
814 struct pollfd plfd;
815 struct input_event ie;
816 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
818 if (This->joyfd==-1)
819 return;
821 while (1)
823 LONG value = 0;
824 int inst_id = -1;
826 plfd.fd = This->joyfd;
827 plfd.events = POLLIN;
829 if (poll(&plfd,1,0) != 1)
830 return;
832 /* we have one event, so we can read */
833 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
834 return;
836 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
837 switch (ie.type) {
838 case EV_KEY: /* button */
840 int btn = This->buttons[ie.code];
842 TRACE("(%p) %d -> %d\n", This, ie.code, btn);
843 if (btn & 0x80)
845 btn &= 0x7F;
846 inst_id = DIDFT_MAKEINSTANCE(btn) | DIDFT_PSHBUTTON;
847 This->generic.js.rgbButtons[btn] = value = ie.value ? 0x80 : 0x00;
849 break;
851 case EV_ABS:
853 int axis = This->dev_axes_to_di[ie.code];
855 /* User axis remapping */
856 if (axis < 0) break;
857 axis = This->generic.axis_map[axis];
858 if (axis < 0) break;
860 inst_id = axis < 8 ? DIDFT_MAKEINSTANCE(axis) | DIDFT_ABSAXIS :
861 DIDFT_MAKEINSTANCE(axis - 8) | DIDFT_POV;
862 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], ie.value);
864 switch (axis) {
865 case 0: This->generic.js.lX = value; break;
866 case 1: This->generic.js.lY = value; break;
867 case 2: This->generic.js.lZ = value; break;
868 case 3: This->generic.js.lRx = value; break;
869 case 4: This->generic.js.lRy = value; break;
870 case 5: This->generic.js.lRz = value; break;
871 case 6: This->generic.js.rglSlider[0] = value; break;
872 case 7: This->generic.js.rglSlider[1] = value; break;
873 case 8: case 9: case 10: case 11:
875 int idx = axis - 8;
877 if (ie.code % 2)
878 This->povs[idx].y = ie.value;
879 else
880 This->povs[idx].x = ie.value;
882 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
883 break;
885 default:
886 FIXME("unhandled joystick axis event (code %d, value %d)\n",ie.code,ie.value);
888 break;
890 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
891 case EV_FF_STATUS:
892 This->ff_state = ie.value;
893 break;
894 #endif
895 #ifdef EV_SYN
896 case EV_SYN:
897 /* there is nothing to do */
898 break;
899 #endif
900 #ifdef EV_MSC
901 case EV_MSC:
902 /* Ignore */
903 break;
904 #endif
905 default:
906 TRACE("skipping event\n");
907 break;
909 if (inst_id >= 0)
910 queue_event(iface, inst_id,
911 value, GetCurrentTime(), This->generic.base.dinput->evsequence++);
915 /******************************************************************************
916 * SetProperty : change input device properties
918 static HRESULT WINAPI JoystickWImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
920 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
922 if (!ph) {
923 WARN("invalid argument\n");
924 return DIERR_INVALIDPARAM;
927 TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
928 TRACE("ph.dwSize = %d, ph.dwHeaderSize =%d, ph.dwObj = %d, ph.dwHow= %d\n",
929 ph->dwSize, ph->dwHeaderSize, ph->dwObj, ph->dwHow);
931 if (IS_DIPROP(rguid)) {
932 switch (LOWORD(rguid)) {
933 case (DWORD_PTR)DIPROP_AUTOCENTER: {
934 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
936 TRACE("autocenter(%d)\n", pd->dwData);
937 This->ff_autocenter = pd->dwData == DIPROPAUTOCENTER_ON;
939 break;
941 case (DWORD_PTR)DIPROP_FFGAIN: {
942 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
944 TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
945 This->ff_gain = MulDiv(pd->dwData, 0xFFFF, 10000);
946 if (This->generic.base.acquired) {
947 /* Update immediately. */
948 struct input_event event;
950 event.type = EV_FF;
951 event.code = FF_GAIN;
952 event.value = This->ff_gain;
953 if (write(This->joyfd, &event, sizeof(event)) == -1)
954 ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
956 break;
958 default:
959 return JoystickWGenericImpl_SetProperty(iface, rguid, ph);
962 return DI_OK;
965 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
967 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
968 return JoystickWImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
971 /******************************************************************************
972 * GetProperty : get input device properties
974 static HRESULT WINAPI JoystickWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
976 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
978 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(rguid), pdiph);
979 _dump_DIPROPHEADER(pdiph);
981 if (!IS_DIPROP(rguid)) return DI_OK;
983 switch (LOWORD(rguid)) {
984 case (DWORD_PTR) DIPROP_AUTOCENTER:
986 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
988 pd->dwData = This->ff_autocenter ? DIPROPAUTOCENTER_ON : DIPROPAUTOCENTER_OFF;
989 TRACE("autocenter(%d)\n", pd->dwData);
990 break;
992 case (DWORD_PTR) DIPROP_FFGAIN:
994 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
996 pd->dwData = MulDiv(This->ff_gain, 10000, 0xFFFF);
997 TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
998 break;
1001 case (DWORD_PTR) DIPROP_VIDPID:
1003 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1005 if (!This->joydev->product_id || !This->joydev->vendor_id)
1006 return DIERR_UNSUPPORTED;
1007 pd->dwData = MAKELONG(This->joydev->vendor_id, This->joydev->product_id);
1008 TRACE("DIPROP_VIDPID(%08x)\n", pd->dwData);
1009 break;
1012 case (DWORD_PTR) DIPROP_JOYSTICKID:
1014 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1016 pd->dwData = get_joystick_index(&This->generic.base.guid);
1017 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
1018 break;
1021 case (DWORD_PTR) DIPROP_GUIDANDPATH:
1023 static const WCHAR formatW[] = {'\\','\\','?','\\','h','i','d','#','v','i','d','_','%','0','4','x','&',
1024 'p','i','d','_','%','0','4','x','&','%','s','_','%','h','u',0};
1025 static const WCHAR miW[] = {'m','i',0};
1026 static const WCHAR igW[] = {'i','g',0};
1028 BOOL is_gamepad;
1029 LPDIPROPGUIDANDPATH pd = (LPDIPROPGUIDANDPATH)pdiph;
1030 WORD vid = This->joydev->vendor_id;
1031 WORD pid = This->joydev->product_id;
1033 if (!pid || !vid)
1034 return DIERR_UNSUPPORTED;
1036 is_gamepad = is_xinput_device(&This->generic.devcaps, vid, pid);
1037 pd->guidClass = GUID_DEVCLASS_HIDCLASS;
1038 sprintfW(pd->wszPath, formatW, vid, pid, is_gamepad ? igW : miW, get_joystick_index(&This->generic.base.guid));
1040 TRACE("DIPROP_GUIDANDPATH(%s, %s): returning fake path\n", debugstr_guid(&pd->guidClass), debugstr_w(pd->wszPath));
1041 break;
1044 default:
1045 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
1048 return DI_OK;
1051 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
1053 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1054 return JoystickWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
1057 /******************************************************************************
1058 * CreateEffect - Create a new FF effect with the specified params
1060 static HRESULT WINAPI JoystickWImpl_CreateEffect(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid,
1061 LPCDIEFFECT lpeff, LPDIRECTINPUTEFFECT *ppdef,
1062 LPUNKNOWN pUnkOuter)
1064 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1065 effect_list_item* new_effect = NULL;
1066 HRESULT retval = DI_OK;
1067 #endif
1069 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1070 TRACE("(this=%p,%p,%p,%p,%p)\n", This, rguid, lpeff, ppdef, pUnkOuter);
1072 *ppdef = NULL;
1073 if (!This->joydev->has_ff)
1075 TRACE("No force feedback support\n");
1076 return DIERR_UNSUPPORTED;
1079 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
1080 TRACE("not available (compiled w/o force feedback support)\n");
1081 return DIERR_UNSUPPORTED;
1082 #else
1084 if (!(new_effect = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_effect))))
1085 return DIERR_OUTOFMEMORY;
1087 retval = linuxinput_create_effect(&This->joyfd, rguid, &new_effect->entry, &new_effect->ref);
1088 if (retval != DI_OK)
1090 HeapFree(GetProcessHeap(), 0, new_effect);
1091 return retval;
1094 if (lpeff != NULL)
1096 retval = IDirectInputEffect_SetParameters(new_effect->ref, lpeff,
1097 DIEP_AXES | DIEP_DIRECTION | DIEP_DURATION | DIEP_ENVELOPE |
1098 DIEP_GAIN | DIEP_SAMPLEPERIOD | DIEP_STARTDELAY | DIEP_TRIGGERBUTTON |
1099 DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS);
1101 if (retval != DI_OK && retval != DI_DOWNLOADSKIPPED)
1103 HeapFree(GetProcessHeap(), 0, new_effect);
1104 return retval;
1108 list_add_tail(&This->ff_effects, &new_effect->entry);
1109 *ppdef = new_effect->ref;
1111 if (pUnkOuter != NULL)
1112 FIXME("Interface aggregation not implemented.\n");
1114 return DI_OK;
1116 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */
1119 static HRESULT WINAPI JoystickAImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid,
1120 LPCDIEFFECT lpeff, LPDIRECTINPUTEFFECT *ppdef,
1121 LPUNKNOWN pUnkOuter)
1123 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1124 return JoystickWImpl_CreateEffect(IDirectInputDevice8W_from_impl(This), rguid, lpeff, ppdef, pUnkOuter);
1127 /*******************************************************************************
1128 * EnumEffects - Enumerate available FF effects
1130 static HRESULT WINAPI JoystickAImpl_EnumEffects(LPDIRECTINPUTDEVICE8A iface,
1131 LPDIENUMEFFECTSCALLBACKA lpCallback,
1132 LPVOID pvRef,
1133 DWORD dwEffType)
1135 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1136 DIEFFECTINFOA dei; /* feif */
1137 DWORD type = DIEFT_GETTYPE(dwEffType);
1138 JoystickImpl* This = impl_from_IDirectInputDevice8A(iface);
1140 TRACE("(this=%p,%p,%d) type=%d\n", This, pvRef, dwEffType, type);
1142 dei.dwSize = sizeof(DIEFFECTINFOA);
1144 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1145 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1146 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1147 (*lpCallback)(&dei, pvRef);
1150 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1151 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1152 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1153 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1154 (*lpCallback)(&dei, pvRef);
1156 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1157 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1158 (*lpCallback)(&dei, pvRef);
1160 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1161 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1162 (*lpCallback)(&dei, pvRef);
1164 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1165 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1166 (*lpCallback)(&dei, pvRef);
1168 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1169 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1170 (*lpCallback)(&dei, pvRef);
1174 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1175 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1176 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1177 (*lpCallback)(&dei, pvRef);
1180 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1181 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1182 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1183 (*lpCallback)(&dei, pvRef);
1185 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1186 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1187 (*lpCallback)(&dei, pvRef);
1189 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1190 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1191 (*lpCallback)(&dei, pvRef);
1193 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1194 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1195 (*lpCallback)(&dei, pvRef);
1199 #endif
1201 return DI_OK;
1204 static HRESULT WINAPI JoystickWImpl_EnumEffects(LPDIRECTINPUTDEVICE8W iface,
1205 LPDIENUMEFFECTSCALLBACKW lpCallback,
1206 LPVOID pvRef,
1207 DWORD dwEffType)
1209 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1210 /* seems silly to duplicate all this code but all the structures and functions
1211 * are actually different (A/W) */
1212 DIEFFECTINFOW dei; /* feif */
1213 DWORD type = DIEFT_GETTYPE(dwEffType);
1214 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1215 int xfd = This->joyfd;
1217 TRACE("(this=%p,%p,%d) type=%d fd=%d\n", This, pvRef, dwEffType, type, xfd);
1219 dei.dwSize = sizeof(DIEFFECTINFOW);
1221 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1222 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1223 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1224 (*lpCallback)(&dei, pvRef);
1227 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1228 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1229 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1230 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1231 (*lpCallback)(&dei, pvRef);
1233 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1234 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1235 (*lpCallback)(&dei, pvRef);
1237 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1238 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1239 (*lpCallback)(&dei, pvRef);
1241 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1242 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1243 (*lpCallback)(&dei, pvRef);
1245 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1246 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1247 (*lpCallback)(&dei, pvRef);
1251 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1252 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1253 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1254 (*lpCallback)(&dei, pvRef);
1257 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1258 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1259 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1260 (*lpCallback)(&dei, pvRef);
1262 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1263 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1264 (*lpCallback)(&dei, pvRef);
1266 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1267 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1268 (*lpCallback)(&dei, pvRef);
1270 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1271 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1272 (*lpCallback)(&dei, pvRef);
1276 /* return to unacquired state if that's where it was */
1277 if (xfd == -1)
1278 IDirectInputDevice8_Unacquire(iface);
1279 #endif
1281 return DI_OK;
1284 /*******************************************************************************
1285 * GetEffectInfo - Get information about a particular effect
1287 static HRESULT WINAPI JoystickAImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8A iface,
1288 LPDIEFFECTINFOA pdei,
1289 REFGUID guid)
1291 JoystickImpl* This = impl_from_IDirectInputDevice8A(iface);
1293 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1295 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1296 return linuxinput_get_info_A(This->joyfd, guid, pdei);
1297 #else
1298 return DI_OK;
1299 #endif
1302 static HRESULT WINAPI JoystickWImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8W iface,
1303 LPDIEFFECTINFOW pdei,
1304 REFGUID guid)
1306 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1308 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1310 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1311 return linuxinput_get_info_W(This->joyfd, guid, pdei);
1312 #else
1313 return DI_OK;
1314 #endif
1317 /*******************************************************************************
1318 * GetForceFeedbackState - Get information about the device's FF state
1320 static HRESULT WINAPI JoystickWImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8W iface, LPDWORD pdwOut)
1322 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1324 TRACE("(this=%p,%p)\n", This, pdwOut);
1326 (*pdwOut) = 0;
1328 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1329 /* DIGFFS_STOPPED is the only mandatory flag to report */
1330 if (This->ff_state == FF_STATUS_STOPPED)
1331 (*pdwOut) |= DIGFFS_STOPPED;
1332 #endif
1334 return DI_OK;
1337 static HRESULT WINAPI JoystickAImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8A iface, LPDWORD pdwOut)
1339 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1340 return JoystickWImpl_GetForceFeedbackState(IDirectInputDevice8W_from_impl(This), pdwOut);
1343 /*******************************************************************************
1344 * SendForceFeedbackCommand - Send a command to the device's FF system
1346 static HRESULT WINAPI JoystickWImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8W iface, DWORD dwFlags)
1348 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1349 TRACE("(this=%p,%d)\n", This, dwFlags);
1351 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1352 switch (dwFlags)
1354 case DISFFC_STOPALL:
1356 effect_list_item *itr;
1358 /* Stop all effects */
1359 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry)
1360 IDirectInputEffect_Stop(itr->ref);
1361 break;
1364 case DISFFC_RESET:
1366 effect_list_item *itr;
1368 /* Stop and unload all effects. It is not true that effects are released */
1369 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry)
1371 IDirectInputEffect_Stop(itr->ref);
1372 IDirectInputEffect_Unload(itr->ref);
1374 break;
1376 case DISFFC_PAUSE:
1377 case DISFFC_CONTINUE:
1378 FIXME("No support for Pause or Continue in linux\n");
1379 break;
1381 case DISFFC_SETACTUATORSOFF:
1382 case DISFFC_SETACTUATORSON:
1383 FIXME("No direct actuator control in linux\n");
1384 break;
1386 default:
1387 WARN("Unknown Force Feedback Command %u!\n", dwFlags);
1388 return DIERR_INVALIDPARAM;
1390 return DI_OK;
1391 #else
1392 return DIERR_UNSUPPORTED;
1393 #endif
1396 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8A iface, DWORD dwFlags)
1398 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1399 return JoystickWImpl_SendForceFeedbackCommand(IDirectInputDevice8W_from_impl(This), dwFlags);
1402 /*******************************************************************************
1403 * EnumCreatedEffectObjects - Enumerate all the effects that have been
1404 * created for this device.
1406 static HRESULT WINAPI JoystickWImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8W iface,
1407 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1408 LPVOID pvRef, DWORD dwFlags)
1410 /* this function is safe to call on non-ff-enabled builds */
1411 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1412 effect_list_item *itr, *ptr;
1414 TRACE("(this=%p,%p,%p,%d)\n", This, lpCallback, pvRef, dwFlags);
1416 if (!lpCallback)
1417 return DIERR_INVALIDPARAM;
1419 if (dwFlags != 0)
1420 FIXME("Flags specified, but no flags exist yet (DX9)!\n");
1422 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1423 (*lpCallback)(itr->ref, pvRef);
1425 return DI_OK;
1428 static HRESULT WINAPI JoystickAImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8A iface,
1429 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1430 LPVOID pvRef, DWORD dwFlags)
1432 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1433 return JoystickWImpl_EnumCreatedEffectObjects(IDirectInputDevice8W_from_impl(This), lpCallback, pvRef, dwFlags);
1436 /******************************************************************************
1437 * GetDeviceInfo : get information about a device's identity
1439 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface,
1440 LPDIDEVICEINSTANCEA pdidi)
1442 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1444 TRACE("(%p) %p\n", This, pdidi);
1446 if (pdidi == NULL) return E_POINTER;
1447 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1448 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)))
1449 return DIERR_INVALIDPARAM;
1451 fill_joystick_dideviceinstanceA(pdidi, This->generic.base.dinput->dwVersion,
1452 get_joystick_index(&This->generic.base.guid));
1453 return DI_OK;
1456 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface,
1457 LPDIDEVICEINSTANCEW pdidi)
1459 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
1461 TRACE("(%p) %p\n", This, pdidi);
1463 if (pdidi == NULL) return E_POINTER;
1464 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1465 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)))
1466 return DIERR_INVALIDPARAM;
1468 fill_joystick_dideviceinstanceW(pdidi, This->generic.base.dinput->dwVersion,
1469 get_joystick_index(&This->generic.base.guid));
1470 return DI_OK;
1473 static const IDirectInputDevice8AVtbl JoystickAvt =
1475 IDirectInputDevice2AImpl_QueryInterface,
1476 IDirectInputDevice2AImpl_AddRef,
1477 IDirectInputDevice2AImpl_Release,
1478 JoystickAGenericImpl_GetCapabilities,
1479 IDirectInputDevice2AImpl_EnumObjects,
1480 JoystickAImpl_GetProperty,
1481 JoystickAImpl_SetProperty,
1482 JoystickAImpl_Acquire,
1483 JoystickAImpl_Unacquire,
1484 JoystickAGenericImpl_GetDeviceState,
1485 IDirectInputDevice2AImpl_GetDeviceData,
1486 IDirectInputDevice2AImpl_SetDataFormat,
1487 IDirectInputDevice2AImpl_SetEventNotification,
1488 IDirectInputDevice2AImpl_SetCooperativeLevel,
1489 JoystickAGenericImpl_GetObjectInfo,
1490 JoystickAImpl_GetDeviceInfo,
1491 IDirectInputDevice2AImpl_RunControlPanel,
1492 IDirectInputDevice2AImpl_Initialize,
1493 JoystickAImpl_CreateEffect,
1494 JoystickAImpl_EnumEffects,
1495 JoystickAImpl_GetEffectInfo,
1496 JoystickAImpl_GetForceFeedbackState,
1497 JoystickAImpl_SendForceFeedbackCommand,
1498 JoystickAImpl_EnumCreatedEffectObjects,
1499 IDirectInputDevice2AImpl_Escape,
1500 JoystickAGenericImpl_Poll,
1501 IDirectInputDevice2AImpl_SendDeviceData,
1502 IDirectInputDevice7AImpl_EnumEffectsInFile,
1503 IDirectInputDevice7AImpl_WriteEffectToFile,
1504 JoystickAGenericImpl_BuildActionMap,
1505 JoystickAGenericImpl_SetActionMap,
1506 IDirectInputDevice8AImpl_GetImageInfo
1509 static const IDirectInputDevice8WVtbl JoystickWvt =
1511 IDirectInputDevice2WImpl_QueryInterface,
1512 IDirectInputDevice2WImpl_AddRef,
1513 IDirectInputDevice2WImpl_Release,
1514 JoystickWGenericImpl_GetCapabilities,
1515 IDirectInputDevice2WImpl_EnumObjects,
1516 JoystickWImpl_GetProperty,
1517 JoystickWImpl_SetProperty,
1518 JoystickWImpl_Acquire,
1519 JoystickWImpl_Unacquire,
1520 JoystickWGenericImpl_GetDeviceState,
1521 IDirectInputDevice2WImpl_GetDeviceData,
1522 IDirectInputDevice2WImpl_SetDataFormat,
1523 IDirectInputDevice2WImpl_SetEventNotification,
1524 IDirectInputDevice2WImpl_SetCooperativeLevel,
1525 JoystickWGenericImpl_GetObjectInfo,
1526 JoystickWImpl_GetDeviceInfo,
1527 IDirectInputDevice2WImpl_RunControlPanel,
1528 IDirectInputDevice2WImpl_Initialize,
1529 JoystickWImpl_CreateEffect,
1530 JoystickWImpl_EnumEffects,
1531 JoystickWImpl_GetEffectInfo,
1532 JoystickWImpl_GetForceFeedbackState,
1533 JoystickWImpl_SendForceFeedbackCommand,
1534 JoystickWImpl_EnumCreatedEffectObjects,
1535 IDirectInputDevice2WImpl_Escape,
1536 JoystickWGenericImpl_Poll,
1537 IDirectInputDevice2WImpl_SendDeviceData,
1538 IDirectInputDevice7WImpl_EnumEffectsInFile,
1539 IDirectInputDevice7WImpl_WriteEffectToFile,
1540 JoystickWGenericImpl_BuildActionMap,
1541 JoystickWGenericImpl_SetActionMap,
1542 IDirectInputDevice8WImpl_GetImageInfo
1545 #else /* HAS_PROPER_HEADER */
1547 const struct dinput_device joystick_linuxinput_device = {
1548 "Wine Linux-input joystick driver",
1549 NULL,
1550 NULL,
1551 NULL
1554 #endif /* HAS_PROPER_HEADER */