mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / dinput / joystick_linuxinput.c
blobf0d2e0dc32bb86f3efed078ee20e411cd4338030
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;
615 INT i;
617 wine_joystick.Data3 = 0;
618 dev_guid.Data3 = 0;
620 /* for the standard joystick GUID use index 0 */
621 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
623 /* for the wine joystick GUIDs use the index stored in Data3 */
624 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3 - DInput_Wine_Joystick_Base_GUID.Data3;
626 for(i = 0; i < have_joydevs; i++)
627 if(IsEqualGUID(&joydevs[i].guid_product, guid)) return i;
629 return MAX_JOYDEV;
632 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
634 unsigned short index;
636 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
637 find_joydevs();
638 *pdev = NULL;
640 if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
641 have_joydevs && index < have_joydevs)
643 JoystickImpl *This;
645 if (riid == NULL)
646 ;/* nothing */
647 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
648 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
649 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
650 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
652 unicode = 0;
654 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
655 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
656 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
657 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
659 unicode = 1;
661 else
663 WARN("no interface\n");
664 return DIERR_NOINTERFACE;
667 This = alloc_device(rguid, dinput, index);
668 TRACE("Created a Joystick device (%p)\n", This);
670 if (!This) return DIERR_OUTOFMEMORY;
672 if (unicode)
673 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
674 else
675 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
677 return DI_OK;
680 return DIERR_DEVICENOTREG;
684 const struct dinput_device joystick_linuxinput_device = {
685 "Wine Linux-input joystick driver",
686 joydev_enum_deviceA,
687 joydev_enum_deviceW,
688 joydev_create_device
691 /******************************************************************************
692 * Acquire : gets exclusive control of the joystick
694 static HRESULT WINAPI JoystickWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
696 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
697 HRESULT res;
699 TRACE("(this=%p)\n",This);
701 if ((res = IDirectInputDevice2WImpl_Acquire(iface)) != DI_OK)
703 WARN("Failed to acquire: %x\n", res);
704 return res;
707 if ((This->joyfd = open(This->joydev->device, O_RDWR)) == -1)
709 if ((This->joyfd = open(This->joydev->device, O_RDONLY)) == -1)
711 /* Couldn't open the device at all */
712 ERR("Failed to open device %s: %d %s\n", This->joydev->device, errno, strerror(errno));
713 IDirectInputDevice2WImpl_Unacquire(iface);
714 return DIERR_NOTFOUND;
716 else
718 /* Couldn't open in r/w but opened in read-only. */
719 WARN("Could not open %s in read-write mode. Force feedback will be disabled.\n", This->joydev->device);
722 else
724 struct input_event event;
726 event.type = EV_FF;
727 event.code = FF_GAIN;
728 event.value = This->ff_gain;
729 if (write(This->joyfd, &event, sizeof(event)) == -1)
730 ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
731 if (!This->ff_autocenter)
733 /* Disable autocenter. */
734 event.code = FF_AUTOCENTER;
735 event.value = 0;
736 if (write(This->joyfd, &event, sizeof(event)) == -1)
737 ERR("Failed disabling autocenter: %d %s\n", errno, strerror(errno));
741 return DI_OK;
744 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
746 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
747 return JoystickWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
750 /******************************************************************************
751 * Unacquire : frees the joystick
753 static HRESULT WINAPI JoystickWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
755 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
756 HRESULT res;
758 TRACE("(this=%p)\n",This);
759 res = IDirectInputDevice2WImpl_Unacquire(iface);
760 if (res==DI_OK && This->joyfd!=-1) {
761 struct input_event event;
763 /* Stop and unload all effects */
764 JoystickWImpl_SendForceFeedbackCommand(iface, DISFFC_RESET);
766 /* Enable autocenter. */
767 event.type = EV_FF;
768 event.code = FF_AUTOCENTER;
769 /* TODO: Read autocenter strength before disabling it, and use it here
770 * instead of 0xFFFF (maximum strength).
772 event.value = 0xFFFF;
773 if (write(This->joyfd, &event, sizeof(event)) == -1)
774 ERR("Failed to set autocenter to %04x: %d %s\n", event.value, errno, strerror(errno));
776 close(This->joyfd);
777 This->joyfd = -1;
779 return res;
782 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
784 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
785 return JoystickWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
789 * set the current state of the js device as it would be with the middle
790 * values on the axes
792 #define CENTER_AXIS(a) \
793 (ji->dev_axes_to_di[a] == -1 ? 0 : joystick_map_axis( &ji->generic.props[ji->dev_axes_to_di[a]], \
794 ji->joydev->axes[a].value ))
795 static void fake_current_js_state(JoystickImpl *ji)
797 int i;
799 /* center the axes */
800 ji->generic.js.lX = CENTER_AXIS(ABS_X);
801 ji->generic.js.lY = CENTER_AXIS(ABS_Y);
802 ji->generic.js.lZ = CENTER_AXIS(ABS_Z);
803 ji->generic.js.lRx = CENTER_AXIS(ABS_RX);
804 ji->generic.js.lRy = CENTER_AXIS(ABS_RY);
805 ji->generic.js.lRz = CENTER_AXIS(ABS_RZ);
806 ji->generic.js.rglSlider[0] = CENTER_AXIS(ABS_THROTTLE);
807 ji->generic.js.rglSlider[1] = CENTER_AXIS(ABS_RUDDER);
809 /* POV center is -1 */
810 for (i = 0; i < 4; i++)
811 ji->generic.js.rgdwPOV[i] = -1;
813 #undef CENTER_AXIS
815 /* convert wine format offset to user format object index */
816 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
818 struct pollfd plfd;
819 struct input_event ie;
820 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
822 if (This->joyfd==-1)
823 return;
825 while (1)
827 LONG value = 0;
828 int inst_id = -1;
830 plfd.fd = This->joyfd;
831 plfd.events = POLLIN;
833 if (poll(&plfd,1,0) != 1)
834 return;
836 /* we have one event, so we can read */
837 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
838 return;
840 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
841 switch (ie.type) {
842 case EV_KEY: /* button */
844 int btn = This->buttons[ie.code];
846 TRACE("(%p) %d -> %d\n", This, ie.code, btn);
847 if (btn & 0x80)
849 btn &= 0x7F;
850 inst_id = DIDFT_MAKEINSTANCE(btn) | DIDFT_PSHBUTTON;
851 This->generic.js.rgbButtons[btn] = value = ie.value ? 0x80 : 0x00;
853 break;
855 case EV_ABS:
857 int axis = This->dev_axes_to_di[ie.code];
859 /* User axis remapping */
860 if (axis < 0) break;
861 axis = This->generic.axis_map[axis];
862 if (axis < 0) break;
864 inst_id = axis < 8 ? DIDFT_MAKEINSTANCE(axis) | DIDFT_ABSAXIS :
865 DIDFT_MAKEINSTANCE(axis - 8) | DIDFT_POV;
866 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], ie.value);
868 switch (axis) {
869 case 0: This->generic.js.lX = value; break;
870 case 1: This->generic.js.lY = value; break;
871 case 2: This->generic.js.lZ = value; break;
872 case 3: This->generic.js.lRx = value; break;
873 case 4: This->generic.js.lRy = value; break;
874 case 5: This->generic.js.lRz = value; break;
875 case 6: This->generic.js.rglSlider[0] = value; break;
876 case 7: This->generic.js.rglSlider[1] = value; break;
877 case 8: case 9: case 10: case 11:
879 int idx = axis - 8;
881 if (ie.code % 2)
882 This->povs[idx].y = ie.value;
883 else
884 This->povs[idx].x = ie.value;
886 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
887 break;
889 default:
890 FIXME("unhandled joystick axis event (code %d, value %d)\n",ie.code,ie.value);
892 break;
894 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
895 case EV_FF_STATUS:
896 This->ff_state = ie.value;
897 break;
898 #endif
899 #ifdef EV_SYN
900 case EV_SYN:
901 /* there is nothing to do */
902 break;
903 #endif
904 #ifdef EV_MSC
905 case EV_MSC:
906 /* Ignore */
907 break;
908 #endif
909 default:
910 TRACE("skipping event\n");
911 break;
913 if (inst_id >= 0)
914 queue_event(iface, inst_id,
915 value, GetCurrentTime(), This->generic.base.dinput->evsequence++);
919 /******************************************************************************
920 * SetProperty : change input device properties
922 static HRESULT WINAPI JoystickWImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
924 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
926 if (!ph) {
927 WARN("invalid argument\n");
928 return DIERR_INVALIDPARAM;
931 TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
932 TRACE("ph.dwSize = %d, ph.dwHeaderSize =%d, ph.dwObj = %d, ph.dwHow= %d\n",
933 ph->dwSize, ph->dwHeaderSize, ph->dwObj, ph->dwHow);
935 if (IS_DIPROP(rguid)) {
936 switch (LOWORD(rguid)) {
937 case (DWORD_PTR)DIPROP_AUTOCENTER: {
938 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
940 TRACE("autocenter(%d)\n", pd->dwData);
941 This->ff_autocenter = pd->dwData == DIPROPAUTOCENTER_ON;
943 break;
945 case (DWORD_PTR)DIPROP_FFGAIN: {
946 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
948 TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
949 This->ff_gain = MulDiv(pd->dwData, 0xFFFF, 10000);
950 if (This->generic.base.acquired) {
951 /* Update immediately. */
952 struct input_event event;
954 event.type = EV_FF;
955 event.code = FF_GAIN;
956 event.value = This->ff_gain;
957 if (write(This->joyfd, &event, sizeof(event)) == -1)
958 ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
960 break;
962 default:
963 return JoystickWGenericImpl_SetProperty(iface, rguid, ph);
966 return DI_OK;
969 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
971 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
972 return JoystickWImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
975 /******************************************************************************
976 * GetProperty : get input device properties
978 static HRESULT WINAPI JoystickWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
980 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
982 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(rguid), pdiph);
983 _dump_DIPROPHEADER(pdiph);
985 if (!IS_DIPROP(rguid)) return DI_OK;
987 switch (LOWORD(rguid)) {
988 case (DWORD_PTR) DIPROP_AUTOCENTER:
990 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
992 pd->dwData = This->ff_autocenter ? DIPROPAUTOCENTER_ON : DIPROPAUTOCENTER_OFF;
993 TRACE("autocenter(%d)\n", pd->dwData);
994 break;
996 case (DWORD_PTR) DIPROP_FFGAIN:
998 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1000 pd->dwData = MulDiv(This->ff_gain, 10000, 0xFFFF);
1001 TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
1002 break;
1005 case (DWORD_PTR) DIPROP_VIDPID:
1007 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1009 if (!This->joydev->product_id || !This->joydev->vendor_id)
1010 return DIERR_UNSUPPORTED;
1011 pd->dwData = MAKELONG(This->joydev->vendor_id, This->joydev->product_id);
1012 TRACE("DIPROP_VIDPID(%08x)\n", pd->dwData);
1013 break;
1016 case (DWORD_PTR) DIPROP_JOYSTICKID:
1018 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1020 pd->dwData = get_joystick_index(&This->generic.base.guid);
1021 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
1022 break;
1025 case (DWORD_PTR) DIPROP_GUIDANDPATH:
1027 static const WCHAR formatW[] = {'\\','\\','?','\\','h','i','d','#','v','i','d','_','%','0','4','x','&',
1028 'p','i','d','_','%','0','4','x','&','%','s','_','%','h','u',0};
1029 static const WCHAR miW[] = {'m','i',0};
1030 static const WCHAR igW[] = {'i','g',0};
1032 BOOL is_gamepad;
1033 LPDIPROPGUIDANDPATH pd = (LPDIPROPGUIDANDPATH)pdiph;
1034 WORD vid = This->joydev->vendor_id;
1035 WORD pid = This->joydev->product_id;
1037 if (!pid || !vid)
1038 return DIERR_UNSUPPORTED;
1040 is_gamepad = is_xinput_device(&This->generic.devcaps, vid, pid);
1041 pd->guidClass = GUID_DEVCLASS_HIDCLASS;
1042 sprintfW(pd->wszPath, formatW, vid, pid, is_gamepad ? igW : miW, get_joystick_index(&This->generic.base.guid));
1044 TRACE("DIPROP_GUIDANDPATH(%s, %s): returning fake path\n", debugstr_guid(&pd->guidClass), debugstr_w(pd->wszPath));
1045 break;
1048 default:
1049 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
1052 return DI_OK;
1055 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
1057 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1058 return JoystickWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
1061 /******************************************************************************
1062 * CreateEffect - Create a new FF effect with the specified params
1064 static HRESULT WINAPI JoystickWImpl_CreateEffect(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid,
1065 LPCDIEFFECT lpeff, LPDIRECTINPUTEFFECT *ppdef,
1066 LPUNKNOWN pUnkOuter)
1068 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1069 effect_list_item* new_effect = NULL;
1070 HRESULT retval = DI_OK;
1071 #endif
1073 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1074 TRACE("(this=%p,%p,%p,%p,%p)\n", This, rguid, lpeff, ppdef, pUnkOuter);
1076 *ppdef = NULL;
1077 if (!This->joydev->has_ff)
1079 TRACE("No force feedback support\n");
1080 return DIERR_UNSUPPORTED;
1083 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
1084 TRACE("not available (compiled w/o force feedback support)\n");
1085 return DIERR_UNSUPPORTED;
1086 #else
1088 if (!(new_effect = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_effect))))
1089 return DIERR_OUTOFMEMORY;
1091 retval = linuxinput_create_effect(&This->joyfd, rguid, &new_effect->entry, &new_effect->ref);
1092 if (retval != DI_OK)
1094 HeapFree(GetProcessHeap(), 0, new_effect);
1095 return retval;
1098 if (lpeff != NULL)
1100 retval = IDirectInputEffect_SetParameters(new_effect->ref, lpeff,
1101 DIEP_AXES | DIEP_DIRECTION | DIEP_DURATION | DIEP_ENVELOPE |
1102 DIEP_GAIN | DIEP_SAMPLEPERIOD | DIEP_STARTDELAY | DIEP_TRIGGERBUTTON |
1103 DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS);
1105 if (retval != DI_OK && retval != DI_DOWNLOADSKIPPED)
1107 HeapFree(GetProcessHeap(), 0, new_effect);
1108 return retval;
1112 list_add_tail(&This->ff_effects, &new_effect->entry);
1113 *ppdef = new_effect->ref;
1115 if (pUnkOuter != NULL)
1116 FIXME("Interface aggregation not implemented.\n");
1118 return DI_OK;
1120 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */
1123 static HRESULT WINAPI JoystickAImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid,
1124 LPCDIEFFECT lpeff, LPDIRECTINPUTEFFECT *ppdef,
1125 LPUNKNOWN pUnkOuter)
1127 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1128 return JoystickWImpl_CreateEffect(IDirectInputDevice8W_from_impl(This), rguid, lpeff, ppdef, pUnkOuter);
1131 /*******************************************************************************
1132 * EnumEffects - Enumerate available FF effects
1134 static HRESULT WINAPI JoystickAImpl_EnumEffects(LPDIRECTINPUTDEVICE8A iface,
1135 LPDIENUMEFFECTSCALLBACKA lpCallback,
1136 LPVOID pvRef,
1137 DWORD dwEffType)
1139 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1140 DIEFFECTINFOA dei; /* feif */
1141 DWORD type = DIEFT_GETTYPE(dwEffType);
1142 JoystickImpl* This = impl_from_IDirectInputDevice8A(iface);
1144 TRACE("(this=%p,%p,%d) type=%d\n", This, pvRef, dwEffType, type);
1146 dei.dwSize = sizeof(DIEFFECTINFOA);
1148 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1149 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1150 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1151 (*lpCallback)(&dei, pvRef);
1154 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1155 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1156 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1157 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1158 (*lpCallback)(&dei, pvRef);
1160 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1161 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1162 (*lpCallback)(&dei, pvRef);
1164 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1165 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1166 (*lpCallback)(&dei, pvRef);
1168 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1169 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1170 (*lpCallback)(&dei, pvRef);
1172 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1173 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1174 (*lpCallback)(&dei, pvRef);
1178 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1179 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1180 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1181 (*lpCallback)(&dei, pvRef);
1184 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1185 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1186 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1187 (*lpCallback)(&dei, pvRef);
1189 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1190 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1191 (*lpCallback)(&dei, pvRef);
1193 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1194 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1195 (*lpCallback)(&dei, pvRef);
1197 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1198 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1199 (*lpCallback)(&dei, pvRef);
1203 #endif
1205 return DI_OK;
1208 static HRESULT WINAPI JoystickWImpl_EnumEffects(LPDIRECTINPUTDEVICE8W iface,
1209 LPDIENUMEFFECTSCALLBACKW lpCallback,
1210 LPVOID pvRef,
1211 DWORD dwEffType)
1213 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1214 /* seems silly to duplicate all this code but all the structures and functions
1215 * are actually different (A/W) */
1216 DIEFFECTINFOW dei; /* feif */
1217 DWORD type = DIEFT_GETTYPE(dwEffType);
1218 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1219 int xfd = This->joyfd;
1221 TRACE("(this=%p,%p,%d) type=%d fd=%d\n", This, pvRef, dwEffType, type, xfd);
1223 dei.dwSize = sizeof(DIEFFECTINFOW);
1225 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1226 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1227 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1228 (*lpCallback)(&dei, pvRef);
1231 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1232 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1233 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1234 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1235 (*lpCallback)(&dei, pvRef);
1237 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1238 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1239 (*lpCallback)(&dei, pvRef);
1241 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1242 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1243 (*lpCallback)(&dei, pvRef);
1245 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1246 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1247 (*lpCallback)(&dei, pvRef);
1249 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1250 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1251 (*lpCallback)(&dei, pvRef);
1255 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1256 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1257 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1258 (*lpCallback)(&dei, pvRef);
1261 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1262 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1263 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1264 (*lpCallback)(&dei, pvRef);
1266 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1267 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1268 (*lpCallback)(&dei, pvRef);
1270 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1271 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1272 (*lpCallback)(&dei, pvRef);
1274 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1275 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1276 (*lpCallback)(&dei, pvRef);
1280 /* return to unacquired state if that's where it was */
1281 if (xfd == -1)
1282 IDirectInputDevice8_Unacquire(iface);
1283 #endif
1285 return DI_OK;
1288 /*******************************************************************************
1289 * GetEffectInfo - Get information about a particular effect
1291 static HRESULT WINAPI JoystickAImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8A iface,
1292 LPDIEFFECTINFOA pdei,
1293 REFGUID guid)
1295 JoystickImpl* This = impl_from_IDirectInputDevice8A(iface);
1297 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1299 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1300 return linuxinput_get_info_A(This->joyfd, guid, pdei);
1301 #else
1302 return DI_OK;
1303 #endif
1306 static HRESULT WINAPI JoystickWImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8W iface,
1307 LPDIEFFECTINFOW pdei,
1308 REFGUID guid)
1310 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1312 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1314 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1315 return linuxinput_get_info_W(This->joyfd, guid, pdei);
1316 #else
1317 return DI_OK;
1318 #endif
1321 /*******************************************************************************
1322 * GetForceFeedbackState - Get information about the device's FF state
1324 static HRESULT WINAPI JoystickWImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8W iface, LPDWORD pdwOut)
1326 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1328 TRACE("(this=%p,%p)\n", This, pdwOut);
1330 (*pdwOut) = 0;
1332 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1333 /* DIGFFS_STOPPED is the only mandatory flag to report */
1334 if (This->ff_state == FF_STATUS_STOPPED)
1335 (*pdwOut) |= DIGFFS_STOPPED;
1336 #endif
1338 return DI_OK;
1341 static HRESULT WINAPI JoystickAImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8A iface, LPDWORD pdwOut)
1343 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1344 return JoystickWImpl_GetForceFeedbackState(IDirectInputDevice8W_from_impl(This), pdwOut);
1347 /*******************************************************************************
1348 * SendForceFeedbackCommand - Send a command to the device's FF system
1350 static HRESULT WINAPI JoystickWImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8W iface, DWORD dwFlags)
1352 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1353 TRACE("(this=%p,%d)\n", This, dwFlags);
1355 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1356 switch (dwFlags)
1358 case DISFFC_STOPALL:
1360 effect_list_item *itr;
1362 /* Stop all effects */
1363 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry)
1364 IDirectInputEffect_Stop(itr->ref);
1365 break;
1368 case DISFFC_RESET:
1370 effect_list_item *itr;
1372 /* Stop and unload all effects. It is not true that effects are released */
1373 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry)
1375 IDirectInputEffect_Stop(itr->ref);
1376 IDirectInputEffect_Unload(itr->ref);
1378 break;
1380 case DISFFC_PAUSE:
1381 case DISFFC_CONTINUE:
1382 FIXME("No support for Pause or Continue in linux\n");
1383 break;
1385 case DISFFC_SETACTUATORSOFF:
1386 case DISFFC_SETACTUATORSON:
1387 FIXME("No direct actuator control in linux\n");
1388 break;
1390 default:
1391 WARN("Unknown Force Feedback Command %u!\n", dwFlags);
1392 return DIERR_INVALIDPARAM;
1394 return DI_OK;
1395 #else
1396 return DIERR_UNSUPPORTED;
1397 #endif
1400 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8A iface, DWORD dwFlags)
1402 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1403 return JoystickWImpl_SendForceFeedbackCommand(IDirectInputDevice8W_from_impl(This), dwFlags);
1406 /*******************************************************************************
1407 * EnumCreatedEffectObjects - Enumerate all the effects that have been
1408 * created for this device.
1410 static HRESULT WINAPI JoystickWImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8W iface,
1411 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1412 LPVOID pvRef, DWORD dwFlags)
1414 /* this function is safe to call on non-ff-enabled builds */
1415 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1416 effect_list_item *itr, *ptr;
1418 TRACE("(this=%p,%p,%p,%d)\n", This, lpCallback, pvRef, dwFlags);
1420 if (!lpCallback)
1421 return DIERR_INVALIDPARAM;
1423 if (dwFlags != 0)
1424 FIXME("Flags specified, but no flags exist yet (DX9)!\n");
1426 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1427 (*lpCallback)(itr->ref, pvRef);
1429 return DI_OK;
1432 static HRESULT WINAPI JoystickAImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8A iface,
1433 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1434 LPVOID pvRef, DWORD dwFlags)
1436 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1437 return JoystickWImpl_EnumCreatedEffectObjects(IDirectInputDevice8W_from_impl(This), lpCallback, pvRef, dwFlags);
1440 /******************************************************************************
1441 * GetDeviceInfo : get information about a device's identity
1443 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface,
1444 LPDIDEVICEINSTANCEA pdidi)
1446 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1448 TRACE("(%p) %p\n", This, pdidi);
1450 if (pdidi == NULL) return E_POINTER;
1451 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1452 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)))
1453 return DIERR_INVALIDPARAM;
1455 fill_joystick_dideviceinstanceA(pdidi, This->generic.base.dinput->dwVersion,
1456 get_joystick_index(&This->generic.base.guid));
1458 pdidi->guidInstance = This->generic.base.guid;
1460 return DI_OK;
1463 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface,
1464 LPDIDEVICEINSTANCEW pdidi)
1466 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
1468 TRACE("(%p) %p\n", This, pdidi);
1470 if (pdidi == NULL) return E_POINTER;
1471 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1472 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)))
1473 return DIERR_INVALIDPARAM;
1475 fill_joystick_dideviceinstanceW(pdidi, This->generic.base.dinput->dwVersion,
1476 get_joystick_index(&This->generic.base.guid));
1478 pdidi->guidInstance = This->generic.base.guid;
1480 return DI_OK;
1483 static const IDirectInputDevice8AVtbl JoystickAvt =
1485 IDirectInputDevice2AImpl_QueryInterface,
1486 IDirectInputDevice2AImpl_AddRef,
1487 IDirectInputDevice2AImpl_Release,
1488 JoystickAGenericImpl_GetCapabilities,
1489 IDirectInputDevice2AImpl_EnumObjects,
1490 JoystickAImpl_GetProperty,
1491 JoystickAImpl_SetProperty,
1492 JoystickAImpl_Acquire,
1493 JoystickAImpl_Unacquire,
1494 JoystickAGenericImpl_GetDeviceState,
1495 IDirectInputDevice2AImpl_GetDeviceData,
1496 IDirectInputDevice2AImpl_SetDataFormat,
1497 IDirectInputDevice2AImpl_SetEventNotification,
1498 IDirectInputDevice2AImpl_SetCooperativeLevel,
1499 JoystickAGenericImpl_GetObjectInfo,
1500 JoystickAImpl_GetDeviceInfo,
1501 IDirectInputDevice2AImpl_RunControlPanel,
1502 IDirectInputDevice2AImpl_Initialize,
1503 JoystickAImpl_CreateEffect,
1504 JoystickAImpl_EnumEffects,
1505 JoystickAImpl_GetEffectInfo,
1506 JoystickAImpl_GetForceFeedbackState,
1507 JoystickAImpl_SendForceFeedbackCommand,
1508 JoystickAImpl_EnumCreatedEffectObjects,
1509 IDirectInputDevice2AImpl_Escape,
1510 JoystickAGenericImpl_Poll,
1511 IDirectInputDevice2AImpl_SendDeviceData,
1512 IDirectInputDevice7AImpl_EnumEffectsInFile,
1513 IDirectInputDevice7AImpl_WriteEffectToFile,
1514 JoystickAGenericImpl_BuildActionMap,
1515 JoystickAGenericImpl_SetActionMap,
1516 IDirectInputDevice8AImpl_GetImageInfo
1519 static const IDirectInputDevice8WVtbl JoystickWvt =
1521 IDirectInputDevice2WImpl_QueryInterface,
1522 IDirectInputDevice2WImpl_AddRef,
1523 IDirectInputDevice2WImpl_Release,
1524 JoystickWGenericImpl_GetCapabilities,
1525 IDirectInputDevice2WImpl_EnumObjects,
1526 JoystickWImpl_GetProperty,
1527 JoystickWImpl_SetProperty,
1528 JoystickWImpl_Acquire,
1529 JoystickWImpl_Unacquire,
1530 JoystickWGenericImpl_GetDeviceState,
1531 IDirectInputDevice2WImpl_GetDeviceData,
1532 IDirectInputDevice2WImpl_SetDataFormat,
1533 IDirectInputDevice2WImpl_SetEventNotification,
1534 IDirectInputDevice2WImpl_SetCooperativeLevel,
1535 JoystickWGenericImpl_GetObjectInfo,
1536 JoystickWImpl_GetDeviceInfo,
1537 IDirectInputDevice2WImpl_RunControlPanel,
1538 IDirectInputDevice2WImpl_Initialize,
1539 JoystickWImpl_CreateEffect,
1540 JoystickWImpl_EnumEffects,
1541 JoystickWImpl_GetEffectInfo,
1542 JoystickWImpl_GetForceFeedbackState,
1543 JoystickWImpl_SendForceFeedbackCommand,
1544 JoystickWImpl_EnumCreatedEffectObjects,
1545 IDirectInputDevice2WImpl_Escape,
1546 JoystickWGenericImpl_Poll,
1547 IDirectInputDevice2WImpl_SendDeviceData,
1548 IDirectInputDevice7WImpl_EnumEffectsInFile,
1549 IDirectInputDevice7WImpl_WriteEffectToFile,
1550 JoystickWGenericImpl_BuildActionMap,
1551 JoystickWGenericImpl_SetActionMap,
1552 IDirectInputDevice8WImpl_GetImageInfo
1555 #else /* HAS_PROPER_HEADER */
1557 const struct dinput_device joystick_linuxinput_device = {
1558 "Wine Linux-input joystick driver",
1559 NULL,
1560 NULL,
1561 NULL
1564 #endif /* HAS_PROPER_HEADER */