dinput: Warn if failed to open device.
[wine.git] / dlls / dinput / joystick_linuxinput.c
blobc6f07ac36834ed3ab268fbf5c561d9f67ad53d7b
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_SYS_ERRNO_H
43 # include <sys/errno.h>
44 #endif
45 #ifdef HAVE_LINUX_INPUT_H
46 # include <linux/input.h>
47 # undef SW_MAX
48 # if defined(EVIOCGBIT) && defined(EV_ABS) && defined(BTN_PINKIE)
49 # define HAVE_CORRECT_LINUXINPUT_H
50 # endif
51 #endif
52 #ifdef HAVE_SYS_POLL_H
53 # include <sys/poll.h>
54 #endif
56 #include "wine/debug.h"
57 #include "wine/unicode.h"
58 #include "wine/list.h"
59 #include "windef.h"
60 #include "winbase.h"
61 #include "winerror.h"
62 #include "winreg.h"
63 #include "dinput.h"
65 #include "dinput_private.h"
66 #include "device_private.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
72 * Maps POV x & y event values to a DX "clock" position:
73 * 0
74 * 31500 4500
75 * 27000 -1 9000
76 * 22500 13500
77 * 18000
79 DWORD joystick_map_pov(POINTL *p)
81 if (p->x > 0)
82 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
83 else if (p->x < 0)
84 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
85 else
86 return p->y < 0 ? 0 : !p->y ? -1 : 18000;
90 * This maps the read value (from the input event) to a value in the
91 * 'wanted' range.
92 * Notes:
93 * Dead zone is in % multiplied by a 100 (range 0..10000)
95 LONG joystick_map_axis(ObjProps *props, int val)
97 LONG ret;
98 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
99 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
101 /* Center input */
102 val -= (props->lDevMin + props->lDevMax) / 2;
104 /* Remove dead zone */
105 if (abs( val ) <= dead_zone / 2)
106 val = 0;
107 else
108 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
110 /* Scale and map the value from the device range into the required range */
111 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
112 (props->lMin + props->lMax) / 2;
114 /* Clamp in case or rounding errors */
115 if (ret > props->lMax) ret = props->lMax;
116 else if (ret < props->lMin) ret = props->lMin;
118 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
119 props->lDevMin, dead_zone, props->lDevMax,
120 props->lMin, props->lDeadZone, props->lMax,
121 val, ret );
123 return ret;
126 #ifdef HAVE_CORRECT_LINUXINPUT_H
128 #define EVDEVPREFIX "/dev/input/event"
130 /* Wine joystick driver object instances */
131 #define WINE_JOYSTICK_MAX_AXES 8
132 #define WINE_JOYSTICK_MAX_POVS 4
133 #define WINE_JOYSTICK_MAX_BUTTONS 128
135 struct wine_input_absinfo {
136 LONG value;
137 LONG minimum;
138 LONG maximum;
139 LONG fuzz;
140 LONG flat;
143 /* implemented in effect_linuxinput.c */
144 HRESULT linuxinput_create_effect(int* fd, REFGUID rguid, struct list *parent_list_entry, LPDIRECTINPUTEFFECT* peff);
145 HRESULT linuxinput_get_info_A(int fd, REFGUID rguid, LPDIEFFECTINFOA info);
146 HRESULT linuxinput_get_info_W(int fd, REFGUID rguid, LPDIEFFECTINFOW info);
148 typedef struct JoystickImpl JoystickImpl;
149 static const IDirectInputDevice8AVtbl JoystickAvt;
150 static const IDirectInputDevice8WVtbl JoystickWvt;
152 struct JoyDev {
153 char *device;
154 char *name;
155 GUID guid;
157 int has_ff;
158 int num_effects;
160 /* data returned by EVIOCGBIT for caps, EV_ABS, EV_KEY, and EV_FF */
161 BYTE evbits[(EV_MAX+7)/8];
162 BYTE absbits[(ABS_MAX+7)/8];
163 BYTE keybits[(KEY_MAX+7)/8];
164 BYTE ffbits[(FF_MAX+7)/8];
166 /* data returned by the EVIOCGABS() ioctl */
167 struct wine_input_absinfo axes[ABS_MAX];
170 struct JoystickImpl
172 struct IDirectInputDevice2AImpl base;
174 struct JoyDev *joydev;
176 /* joystick private */
177 int joyfd;
179 DIJOYSTATE2 js;
181 ObjProps props[ABS_MAX];
183 int axes[ABS_MAX];
184 POINTL povs[4];
186 /* LUT for KEY_ to offset in rgbButtons */
187 BYTE buttons[KEY_MAX];
189 DWORD numAxes;
190 DWORD numPOVs;
191 DWORD numButtons;
193 /* Force feedback variables */
194 struct list ff_effects;
195 int ff_state;
198 static void fake_current_js_state(JoystickImpl *ji);
199 static void find_joydevs(void);
201 /* This GUID is slightly different from the linux joystick one. Take note. */
202 static const GUID DInput_Wine_Joystick_Base_GUID = { /* 9e573eda-7734-11d2-8d4a-23903fb6bdf7 */
203 0x9e573eda,
204 0x7734,
205 0x11d2,
206 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
209 #define test_bit(arr,bit) (((BYTE*)(arr))[(bit)>>3]&(1<<((bit)&7)))
211 #define MAX_JOYDEV 64
213 static int have_joydevs = -1;
214 static struct JoyDev *joydevs = NULL;
216 static void find_joydevs(void)
218 int i;
220 if (InterlockedCompareExchange(&have_joydevs, 0, -1) != -1)
221 /* Someone beat us to it */
222 return;
224 for (i = 0; i < MAX_JOYDEV; i++)
226 char buf[MAX_PATH];
227 struct JoyDev joydev = {0};
228 int fd;
229 int no_ff_check = 0;
230 int j;
232 snprintf(buf, sizeof(buf), EVDEVPREFIX"%d", i);
234 if ((fd = open(buf, O_RDWR)) == -1)
236 fd = open(buf, O_RDONLY);
237 no_ff_check = 1;
240 if (fd == -1)
242 WARN("Failed to open \"%s\": %d %s\n", buf, errno, strerror(errno));
243 continue;
246 if ((-1==ioctl(fd,EVIOCGBIT(0,sizeof(joydev.evbits)),joydev.evbits))) {
247 perror("EVIOCGBIT 0");
248 close(fd);
249 continue;
251 if (-1==ioctl(fd,EVIOCGBIT(EV_ABS,sizeof(joydev.absbits)),joydev.absbits)) {
252 perror("EVIOCGBIT EV_ABS");
253 close(fd);
254 continue;
256 if (-1==ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(joydev.keybits)),joydev.keybits)) {
257 perror("EVIOCGBIT EV_KEY");
258 close(fd);
259 continue;
261 /* A true joystick has at least axis X and Y, and at least 1
262 * button. copied from linux/drivers/input/joydev.c */
263 if (test_bit(joydev.absbits,ABS_X) && test_bit(joydev.absbits,ABS_Y) &&
264 ( test_bit(joydev.keybits,BTN_TRIGGER) ||
265 test_bit(joydev.keybits,BTN_A) ||
266 test_bit(joydev.keybits,BTN_1)
270 if (!(joydev.device = HeapAlloc(GetProcessHeap(), 0, strlen(buf) + 1))) {
271 close(fd);
272 continue;
274 strcpy(joydev.device, buf);
276 buf[MAX_PATH - 1] = 0;
277 if (ioctl(fd, EVIOCGNAME(MAX_PATH - 1), buf) != -1 &&
278 (joydev.name = HeapAlloc(GetProcessHeap(), 0, strlen(buf) + 1)))
279 strcpy(joydev.name, buf);
280 else
281 joydev.name = joydev.device;
283 joydev.guid = DInput_Wine_Joystick_Base_GUID;
284 joydev.guid.Data3 += have_joydevs;
286 TRACE("Found a joystick on %s: %s (%s)\n",
287 joydev.device, joydev.name,
288 debugstr_guid(&joydev.guid)
291 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
292 if (!no_ff_check &&
293 test_bit(joydev.evbits, EV_FF) &&
294 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(joydev.ffbits)), joydev.ffbits) != -1 &&
295 ioctl(fd, EVIOCGEFFECTS, &joydev.num_effects) != -1 &&
296 joydev.num_effects > 0)
298 TRACE(" ... with force feedback\n");
299 joydev.has_ff = 1;
301 #endif
303 for (j=0;j<ABS_MAX;j++) {
304 if (test_bit(joydev.absbits,j)) {
305 if (-1!=ioctl(fd,EVIOCGABS(j),&(joydev.axes[j]))) {
306 TRACE(" ... with axis %d: cur=%d, min=%d, max=%d, fuzz=%d, flat=%d\n",
308 joydev.axes[j].value,
309 joydev.axes[j].minimum,
310 joydev.axes[j].maximum,
311 joydev.axes[j].fuzz,
312 joydev.axes[j].flat
318 if (have_joydevs==0) {
319 joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
320 } else {
321 HeapReAlloc(GetProcessHeap(), 0, joydevs, (1+have_joydevs) * sizeof(struct JoyDev));
323 memcpy(joydevs+have_joydevs, &joydev, sizeof(struct JoyDev));
324 have_joydevs++;
327 close(fd);
331 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
333 DWORD dwSize = lpddi->dwSize;
335 TRACE("%d %p\n", dwSize, lpddi);
336 memset(lpddi, 0, dwSize);
338 lpddi->dwSize = dwSize;
339 lpddi->guidInstance = joydevs[id].guid;
340 lpddi->guidProduct = DInput_Wine_Joystick_Base_GUID;
341 lpddi->guidFFDriver = GUID_NULL;
343 if (version >= 0x0800)
344 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
345 else
346 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
348 strcpy(lpddi->tszInstanceName, joydevs[id].name);
349 strcpy(lpddi->tszProductName, joydevs[id].device);
352 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
354 DWORD dwSize = lpddi->dwSize;
356 TRACE("%d %p\n", dwSize, lpddi);
357 memset(lpddi, 0, dwSize);
359 lpddi->dwSize = dwSize;
360 lpddi->guidInstance = joydevs[id].guid;
361 lpddi->guidProduct = DInput_Wine_Joystick_Base_GUID;
362 lpddi->guidFFDriver = GUID_NULL;
364 if (version >= 0x0800)
365 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
366 else
367 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
369 MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
370 MultiByteToWideChar(CP_ACP, 0, joydevs[id].device, -1, lpddi->tszProductName, MAX_PATH);
373 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
375 find_joydevs();
377 if (id >= have_joydevs) {
378 return FALSE;
381 if (!((dwDevType == 0) ||
382 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
383 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
384 return FALSE;
386 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
387 if (dwFlags & DIEDFL_FORCEFEEDBACK)
388 return FALSE;
389 #endif
391 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
392 fill_joystick_dideviceinstanceA(lpddi, version, id);
393 return TRUE;
395 return FALSE;
398 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
400 find_joydevs();
402 if (id >= have_joydevs) {
403 return FALSE;
406 if (!((dwDevType == 0) ||
407 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
408 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
409 return FALSE;
411 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
412 if (dwFlags & DIEDFL_FORCEFEEDBACK)
413 return FALSE;
414 #endif
416 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
417 fill_joystick_dideviceinstanceW(lpddi, version, id);
418 return TRUE;
420 return FALSE;
423 static JoystickImpl *alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput, unsigned short index)
425 JoystickImpl* newDevice;
426 LPDIDATAFORMAT df = NULL;
427 int i, idx = 0;
428 char buffer[MAX_PATH+16];
429 HKEY hkey, appkey;
430 LONG def_deadzone = 0;
432 newDevice = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(JoystickImpl));
433 if (!newDevice) return NULL;
435 newDevice->base.lpVtbl = jvt;
436 newDevice->base.ref = 1;
437 newDevice->base.guid = *rguid;
438 newDevice->base.dinput = dinput;
439 newDevice->joyfd = -1;
440 newDevice->joydev = &joydevs[index];
441 list_init(&newDevice->ff_effects);
442 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
443 newDevice->ff_state = FF_STATUS_STOPPED;
444 #endif
445 InitializeCriticalSection(&newDevice->base.crit);
446 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
448 /* get options */
449 get_app_key(&hkey, &appkey);
451 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH))
453 def_deadzone = atoi(buffer);
454 TRACE("setting default deadzone to: %d\n", def_deadzone);
456 if (appkey) RegCloseKey(appkey);
457 if (hkey) RegCloseKey(hkey);
459 /* Create copy of default data format */
460 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto failed;
461 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
462 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
464 /* Supported Axis & POVs should map 1-to-1 */
465 for (i = 0; i < WINE_JOYSTICK_MAX_AXES; i++)
467 if (!test_bit(newDevice->joydev->absbits, i)) {
468 newDevice->axes[i] = -1;
469 continue;
472 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i], df->dwObjSize);
473 newDevice->axes[i] = idx;
474 newDevice->props[idx].lDevMin = newDevice->joydev->axes[i].minimum;
475 newDevice->props[idx].lDevMax = newDevice->joydev->axes[i].maximum;
476 newDevice->props[idx].lMin = 0;
477 newDevice->props[idx].lMax = 0xffff;
478 newDevice->props[idx].lSaturation = 0;
479 newDevice->props[idx].lDeadZone = def_deadzone;
481 /* Linux supports force-feedback on X & Y axes only */
482 if (newDevice->joydev->has_ff && (i == 0 || i == 1))
483 df->rgodf[idx].dwFlags |= DIDOI_FFACTUATOR;
485 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->numAxes++) | DIDFT_ABSAXIS;
488 for (i = 0; i < WINE_JOYSTICK_MAX_POVS; i++)
490 if (!test_bit(newDevice->joydev->absbits, ABS_HAT0X + i * 2) ||
491 !test_bit(newDevice->joydev->absbits, ABS_HAT0Y + i * 2)) {
492 newDevice->axes[ABS_HAT0X + i * 2] = newDevice->axes[ABS_HAT0Y + i * 2] = -1;
493 continue;
496 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + WINE_JOYSTICK_MAX_AXES], df->dwObjSize);
497 newDevice->axes[ABS_HAT0X + i * 2] = newDevice->axes[ABS_HAT0Y + i * 2] = i;
498 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->numPOVs++) | DIDFT_POV;
501 /* Buttons can be anywhere, so check all */
502 for (i = 0; i < KEY_MAX && newDevice->numButtons < WINE_JOYSTICK_MAX_BUTTONS; i++)
504 if (!test_bit(newDevice->joydev->keybits, i)) continue;
506 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[newDevice->numButtons + WINE_JOYSTICK_MAX_AXES + WINE_JOYSTICK_MAX_POVS], df->dwObjSize);
507 newDevice->buttons[i] = 0x80 | newDevice->numButtons;
508 df->rgodf[idx ].pguid = &GUID_Button;
509 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->numButtons++) | DIDFT_PSHBUTTON;
511 df->dwNumObjs = idx;
513 fake_current_js_state(newDevice);
515 newDevice->base.data_format.wine_df = df;
516 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
517 return newDevice;
519 failed:
520 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
521 HeapFree(GetProcessHeap(), 0, df);
522 HeapFree(GetProcessHeap(), 0, newDevice);
523 return NULL;
526 /******************************************************************************
527 * get_joystick_index : Get the joystick index from a given GUID
529 static unsigned short get_joystick_index(REFGUID guid)
531 GUID wine_joystick = DInput_Wine_Joystick_Base_GUID;
532 GUID dev_guid = *guid;
534 wine_joystick.Data3 = 0;
535 dev_guid.Data3 = 0;
537 /* for the standard joystick GUID use index 0 */
538 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
540 /* for the wine joystick GUIDs use the index stored in Data3 */
541 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3 - DInput_Wine_Joystick_Base_GUID.Data3;
543 return MAX_JOYDEV;
546 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
548 unsigned short index;
550 find_joydevs();
552 if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
553 have_joydevs && index < have_joydevs)
555 if ((riid == NULL) ||
556 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
557 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
558 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
559 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
561 *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &JoystickAvt, dinput, index);
562 TRACE("Created a Joystick device (%p)\n", *pdev);
564 if (*pdev == NULL)
566 ERR("out of memory\n");
567 return DIERR_OUTOFMEMORY;
569 return DI_OK;
572 WARN("no interface\n");
573 return DIERR_NOINTERFACE;
576 return DIERR_DEVICENOTREG;
580 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
582 unsigned short index;
584 find_joydevs();
586 if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
587 have_joydevs && index < have_joydevs)
589 if ((riid == NULL) ||
590 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
591 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
592 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
593 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
595 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &JoystickWvt, dinput, index);
596 TRACE("Created a Joystick device (%p)\n", *pdev);
598 if (*pdev == NULL)
600 ERR("out of memory\n");
601 return DIERR_OUTOFMEMORY;
603 return DI_OK;
605 WARN("no interface\n");
606 return DIERR_NOINTERFACE;
609 WARN("invalid device GUID\n");
610 return DIERR_DEVICENOTREG;
613 const struct dinput_device joystick_linuxinput_device = {
614 "Wine Linux-input joystick driver",
615 joydev_enum_deviceA,
616 joydev_enum_deviceW,
617 joydev_create_deviceA,
618 joydev_create_deviceW
621 /******************************************************************************
622 * Acquire : gets exclusive control of the joystick
624 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
626 JoystickImpl *This = (JoystickImpl *)iface;
627 HRESULT res;
629 TRACE("(this=%p)\n",This);
631 res = IDirectInputDevice2AImpl_Acquire(iface);
632 if (res==DI_OK) {
633 if (-1==(This->joyfd=open(This->joydev->device,O_RDWR))) {
634 if (-1==(This->joyfd=open(This->joydev->device,O_RDONLY))) {
635 /* Couldn't open the device at all */
636 perror(This->joydev->device);
637 IDirectInputDevice2AImpl_Unacquire(iface);
638 return DIERR_NOTFOUND;
639 } else {
640 /* Couldn't open in r/w but opened in read-only. */
641 WARN("Could not open %s in read-write mode. Force feedback will be disabled.\n", This->joydev->device);
645 else
646 WARN("Failed to acquire: %x\n", res);
648 return res;
651 /******************************************************************************
652 * Unacquire : frees the joystick
654 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
656 JoystickImpl *This = (JoystickImpl *)iface;
657 HRESULT res;
659 TRACE("(this=%p)\n",This);
660 res = IDirectInputDevice2AImpl_Unacquire(iface);
661 if (res==DI_OK && This->joyfd!=-1) {
662 close(This->joyfd);
663 This->joyfd = -1;
665 return res;
669 * set the current state of the js device as it would be with the middle
670 * values on the axes
672 #define CENTER_AXIS(a) \
673 (ji->axes[a] == -1 ? 0 : joystick_map_axis( &ji->props[ji->axes[a]], \
674 ji->joydev->axes[a].value ))
675 static void fake_current_js_state(JoystickImpl *ji)
677 int i;
679 /* center the axes */
680 ji->js.lX = CENTER_AXIS(ABS_X);
681 ji->js.lY = CENTER_AXIS(ABS_Y);
682 ji->js.lZ = CENTER_AXIS(ABS_Z);
683 ji->js.lRx = CENTER_AXIS(ABS_RX);
684 ji->js.lRy = CENTER_AXIS(ABS_RY);
685 ji->js.lRz = CENTER_AXIS(ABS_RZ);
686 ji->js.rglSlider[0] = CENTER_AXIS(ABS_THROTTLE);
687 ji->js.rglSlider[1] = CENTER_AXIS(ABS_RUDDER);
689 /* POV center is -1 */
690 for (i = 0; i < 4; i++)
691 ji->js.rgdwPOV[i] = -1;
693 #undef CENTER_AXIS
695 /* convert wine format offset to user format object index */
696 static void joy_polldev(JoystickImpl *This)
698 struct pollfd plfd;
699 struct input_event ie;
701 if (This->joyfd==-1)
702 return;
704 while (1)
706 LONG value = 0;
707 int inst_id = -1;
709 plfd.fd = This->joyfd;
710 plfd.events = POLLIN;
712 if (poll(&plfd,1,0) != 1)
713 return;
715 /* we have one event, so we can read */
716 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
717 return;
719 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
720 switch (ie.type) {
721 case EV_KEY: /* button */
723 int btn = This->buttons[ie.code];
725 TRACE("(%p) %d -> %d\n", This, ie.code, btn);
726 if (btn & 0x80)
728 btn &= 0x7F;
729 inst_id = DIDFT_MAKEINSTANCE(btn) | DIDFT_PSHBUTTON;
730 This->js.rgbButtons[btn] = value = ie.value ? 0x80 : 0x00;
732 break;
734 case EV_ABS:
736 int axis = This->axes[ie.code];
737 if (axis==-1) {
738 break;
740 inst_id = DIDFT_MAKEINSTANCE(axis) | (ie.code < ABS_HAT0X ? DIDFT_ABSAXIS : DIDFT_POV);
741 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], ie.value);
743 switch (ie.code) {
744 case ABS_X: This->js.lX = value; break;
745 case ABS_Y: This->js.lY = value; break;
746 case ABS_Z: This->js.lZ = value; break;
747 case ABS_RX: This->js.lRx = value; break;
748 case ABS_RY: This->js.lRy = value; break;
749 case ABS_RZ: This->js.lRz = value; break;
750 case ABS_THROTTLE: This->js.rglSlider[0] = value; break;
751 case ABS_RUDDER: This->js.rglSlider[1] = value; break;
752 case ABS_HAT0X: case ABS_HAT0Y: case ABS_HAT1X: case ABS_HAT1Y:
753 case ABS_HAT2X: case ABS_HAT2Y: case ABS_HAT3X: case ABS_HAT3Y:
755 int idx = (ie.code - ABS_HAT0X) / 2;
757 if (ie.code % 2)
758 This->povs[idx].y = ie.value;
759 else
760 This->povs[idx].x = ie.value;
762 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
763 break;
765 default:
766 FIXME("unhandled joystick axis event (code %d, value %d)\n",ie.code,ie.value);
768 break;
770 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
771 case EV_FF_STATUS:
772 This->ff_state = ie.value;
773 break;
774 #endif
775 #ifdef EV_SYN
776 case EV_SYN:
777 /* there is nothing to do */
778 break;
779 #endif
780 default:
781 FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
782 break;
784 if (inst_id >= 0)
785 queue_event((LPDIRECTINPUTDEVICE8A)This,
786 id_to_offset(&This->base.data_format, inst_id),
787 value, ie.time.tv_usec, This->base.dinput->evsequence++);
791 /******************************************************************************
792 * GetDeviceState : returns the "state" of the joystick.
795 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
796 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
798 JoystickImpl *This = (JoystickImpl *)iface;
800 TRACE("(this=%p,0x%08x,%p)\n", This, len, ptr);
802 if (!This->base.acquired)
804 WARN("not acquired\n");
805 return DIERR_NOTACQUIRED;
808 joy_polldev(This);
810 /* convert and copy data to user supplied buffer */
811 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
813 return DI_OK;
816 /******************************************************************************
817 * SetProperty : change input device properties
819 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface,
820 REFGUID rguid,
821 LPCDIPROPHEADER ph)
823 JoystickImpl *This = (JoystickImpl *)iface;
825 if (!ph) {
826 WARN("invalid argument\n");
827 return DIERR_INVALIDPARAM;
830 TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
831 TRACE("ph.dwSize = %d, ph.dwHeaderSize =%d, ph.dwObj = %d, ph.dwHow= %d\n",
832 ph->dwSize, ph->dwHeaderSize, ph->dwObj, ph->dwHow);
834 if (!HIWORD(rguid)) {
835 switch (LOWORD(rguid)) {
836 case (DWORD)DIPROP_RANGE: {
837 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
839 if (ph->dwHow == DIPH_DEVICE) {
840 DWORD i;
841 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
842 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
843 /* Scale dead-zone */
844 This->props[i].lDeadZone = MulDiv(This->props[i].lDeadZone, pr->lMax - pr->lMin,
845 This->props[i].lMax - This->props[i].lMin);
846 This->props[i].lMin = pr->lMin;
847 This->props[i].lMax = pr->lMax;
849 } else {
850 int obj = find_property(&This->base.data_format, ph);
852 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
853 if (obj >= 0) {
854 /* Scale dead-zone */
855 This->props[obj].lDeadZone = MulDiv(This->props[obj].lDeadZone, pr->lMax - pr->lMin,
856 This->props[obj].lMax - This->props[obj].lMin);
857 This->props[obj].lMin = pr->lMin;
858 This->props[obj].lMax = pr->lMax;
861 fake_current_js_state(This);
862 break;
864 case (DWORD)DIPROP_DEADZONE: {
865 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
866 if (ph->dwHow == DIPH_DEVICE) {
867 DWORD i;
868 TRACE("deadzone(%d) all\n", pd->dwData);
869 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
870 This->props[i].lDeadZone = pd->dwData;
872 } else {
873 int obj = find_property(&This->base.data_format, ph);
875 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
876 if (obj >= 0) {
877 This->props[obj].lDeadZone = pd->dwData;
880 fake_current_js_state(This);
881 break;
883 case (DWORD)DIPROP_CALIBRATIONMODE: {
884 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
885 FIXME("DIPROP_CALIBRATIONMODE(%d)\n", pd->dwData);
886 break;
888 case (DWORD)DIPROP_AUTOCENTER: {
889 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
891 FIXME("DIPROP_AUTOCENTER(%d)\n", pd->dwData);
892 break;
894 case (DWORD)DIPROP_SATURATION: {
895 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
897 if (ph->dwHow == DIPH_DEVICE) {
898 DWORD i;
900 TRACE("saturation(%d) all\n", pd->dwData);
901 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
902 This->props[i].lSaturation = pd->dwData;
903 } else {
904 int obj = find_property(&This->base.data_format, ph);
906 if (obj < 0) return DIERR_OBJECTNOTFOUND;
908 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
909 This->props[obj].lSaturation = pd->dwData;
911 fake_current_js_state(This);
912 break;
914 default:
915 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
918 return DI_OK;
921 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
922 LPDIRECTINPUTDEVICE8A iface,
923 LPDIDEVCAPS lpDIDevCaps)
925 JoystickImpl *This = (JoystickImpl *)iface;
927 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
929 if (!lpDIDevCaps) {
930 WARN("invalid pointer\n");
931 return E_POINTER;
934 if (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) {
935 WARN("invalid argument\n");
936 return DIERR_INVALIDPARAM;
939 lpDIDevCaps->dwFlags = DIDC_ATTACHED;
940 if (This->base.dinput->dwVersion >= 0x0800)
941 lpDIDevCaps->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
942 else
943 lpDIDevCaps->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
945 if (This->joydev->has_ff)
946 lpDIDevCaps->dwFlags |= DIDC_FORCEFEEDBACK;
948 lpDIDevCaps->dwAxes = This->numAxes;
949 lpDIDevCaps->dwButtons = This->numButtons;
950 lpDIDevCaps->dwPOVs = This->numPOVs;
952 return DI_OK;
955 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
957 JoystickImpl *This = (JoystickImpl *)iface;
959 TRACE("(%p)\n",This);
961 if (!This->base.acquired)
962 return DIERR_NOTACQUIRED;
964 joy_polldev(This);
965 return DI_OK;
968 /******************************************************************************
969 * GetProperty : get input device properties
971 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
972 REFGUID rguid,
973 LPDIPROPHEADER pdiph)
975 JoystickImpl *This = (JoystickImpl *)iface;
977 TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
978 _dump_DIPROPHEADER(pdiph);
980 if (HIWORD(rguid)) return DI_OK;
982 switch (LOWORD(rguid)) {
983 case (DWORD) DIPROP_RANGE:
985 LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
986 int obj = find_property(&This->base.data_format, pdiph);
988 if (obj < 0) return DIERR_OBJECTNOTFOUND;
990 pr->lMin = This->props[obj].lMin;
991 pr->lMax = This->props[obj].lMax;
992 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
993 break;
995 case (DWORD) DIPROP_DEADZONE:
997 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
998 int obj = find_property(&This->base.data_format, pdiph);
1000 if (obj < 0) return DIERR_OBJECTNOTFOUND;
1002 pd->dwData = This->props[obj].lDeadZone;
1003 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
1004 break;
1006 case (DWORD) DIPROP_SATURATION:
1008 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1009 int obj = find_property(&This->base.data_format, pdiph);
1011 if (obj < 0) return DIERR_OBJECTNOTFOUND;
1013 pd->dwData = This->props[obj].lSaturation;
1014 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
1015 break;
1018 default:
1019 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
1022 return DI_OK;
1025 /******************************************************************************
1026 * GetObjectInfo : get information about a device object such as a button
1027 * or axis
1029 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
1030 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
1032 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
1033 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
1034 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
1035 HRESULT res;
1037 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
1038 if (res != DI_OK) return res;
1040 if (pdidoi->dwType & DIDFT_AXIS)
1041 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
1042 else if (pdidoi->dwType & DIDFT_POV)
1043 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
1044 else if (pdidoi->dwType & DIDFT_BUTTON)
1045 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
1047 _dump_OBJECTINSTANCEW(pdidoi);
1048 return res;
1051 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
1052 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
1054 HRESULT res;
1055 DIDEVICEOBJECTINSTANCEW didoiW;
1056 DWORD dwSize = pdidoi->dwSize;
1058 didoiW.dwSize = sizeof(didoiW);
1059 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
1060 if (res != DI_OK) return res;
1062 memset(pdidoi, 0, pdidoi->dwSize);
1063 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
1064 pdidoi->dwSize = dwSize;
1065 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
1066 sizeof(pdidoi->tszName), NULL, NULL);
1068 return res;
1071 /******************************************************************************
1072 * CreateEffect - Create a new FF effect with the specified params
1074 static HRESULT WINAPI JoystickAImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface,
1075 REFGUID rguid,
1076 LPCDIEFFECT lpeff,
1077 LPDIRECTINPUTEFFECT *ppdef,
1078 LPUNKNOWN pUnkOuter)
1080 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1081 effect_list_item* new_effect = NULL;
1082 HRESULT retval = DI_OK;
1083 #endif
1085 JoystickImpl* This = (JoystickImpl*)iface;
1086 TRACE("(this=%p,%p,%p,%p,%p)\n", This, rguid, lpeff, ppdef, pUnkOuter);
1088 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
1089 TRACE("not available (compiled w/o ff support)\n");
1090 *ppdef = NULL;
1091 return DI_OK;
1092 #else
1094 if (!(new_effect = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_effect))))
1095 return DIERR_OUTOFMEMORY;
1097 retval = linuxinput_create_effect(&This->joyfd, rguid, &new_effect->entry, &new_effect->ref);
1098 if (retval != DI_OK)
1100 HeapFree(GetProcessHeap(), 0, new_effect);
1101 return retval;
1104 if (lpeff != NULL)
1106 retval = IDirectInputEffect_SetParameters(new_effect->ref, lpeff, 0);
1108 if (retval != DI_OK && retval != DI_DOWNLOADSKIPPED)
1110 HeapFree(GetProcessHeap(), 0, new_effect);
1111 return retval;
1115 list_add_tail(&This->ff_effects, &new_effect->entry);
1116 *ppdef = new_effect->ref;
1118 if (pUnkOuter != NULL)
1119 FIXME("Interface aggregation not implemented.\n");
1121 return DI_OK;
1123 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */
1126 /*******************************************************************************
1127 * EnumEffects - Enumerate available FF effects
1129 static HRESULT WINAPI JoystickAImpl_EnumEffects(LPDIRECTINPUTDEVICE8A iface,
1130 LPDIENUMEFFECTSCALLBACKA lpCallback,
1131 LPVOID pvRef,
1132 DWORD dwEffType)
1134 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1135 DIEFFECTINFOA dei; /* feif */
1136 DWORD type = DIEFT_GETTYPE(dwEffType);
1137 JoystickImpl* This = (JoystickImpl*)iface;
1139 TRACE("(this=%p,%p,%d) type=%d\n", This, pvRef, dwEffType, type);
1141 dei.dwSize = sizeof(DIEFFECTINFOA);
1143 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1144 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1145 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1146 (*lpCallback)(&dei, pvRef);
1149 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1150 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1151 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1152 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1153 (*lpCallback)(&dei, pvRef);
1155 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1156 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1157 (*lpCallback)(&dei, pvRef);
1159 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1160 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1161 (*lpCallback)(&dei, pvRef);
1163 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1164 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1165 (*lpCallback)(&dei, pvRef);
1167 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1168 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1169 (*lpCallback)(&dei, pvRef);
1173 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1174 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1175 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1176 (*lpCallback)(&dei, pvRef);
1179 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1180 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1181 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1182 (*lpCallback)(&dei, pvRef);
1184 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1185 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1186 (*lpCallback)(&dei, pvRef);
1188 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1189 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1190 (*lpCallback)(&dei, pvRef);
1192 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1193 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1194 (*lpCallback)(&dei, pvRef);
1198 #endif
1200 return DI_OK;
1203 static HRESULT WINAPI JoystickWImpl_EnumEffects(LPDIRECTINPUTDEVICE8W iface,
1204 LPDIENUMEFFECTSCALLBACKW lpCallback,
1205 LPVOID pvRef,
1206 DWORD dwEffType)
1208 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1209 /* seems silly to duplicate all this code but all the structures and functions
1210 * are actually different (A/W) */
1211 DIEFFECTINFOW dei; /* feif */
1212 DWORD type = DIEFT_GETTYPE(dwEffType);
1213 JoystickImpl* This = (JoystickImpl*)iface;
1214 int xfd = This->joyfd;
1216 TRACE("(this=%p,%p,%d) type=%d fd=%d\n", This, pvRef, dwEffType, type, xfd);
1218 dei.dwSize = sizeof(DIEFFECTINFOW);
1220 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1221 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1222 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1223 (*lpCallback)(&dei, pvRef);
1226 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1227 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1228 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1229 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1230 (*lpCallback)(&dei, pvRef);
1232 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1233 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1234 (*lpCallback)(&dei, pvRef);
1236 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1237 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1238 (*lpCallback)(&dei, pvRef);
1240 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1241 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1242 (*lpCallback)(&dei, pvRef);
1244 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1245 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1246 (*lpCallback)(&dei, pvRef);
1250 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1251 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1252 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1253 (*lpCallback)(&dei, pvRef);
1256 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1257 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1258 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1259 (*lpCallback)(&dei, pvRef);
1261 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1262 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1263 (*lpCallback)(&dei, pvRef);
1265 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1266 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1267 (*lpCallback)(&dei, pvRef);
1269 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1270 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1271 (*lpCallback)(&dei, pvRef);
1275 /* return to unacquired state if that's where it was */
1276 if (xfd == -1)
1277 IDirectInputDevice8_Unacquire(iface);
1278 #endif
1280 return DI_OK;
1283 /*******************************************************************************
1284 * GetEffectInfo - Get information about a particular effect
1286 static HRESULT WINAPI JoystickAImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8A iface,
1287 LPDIEFFECTINFOA pdei,
1288 REFGUID guid)
1290 JoystickImpl* This = (JoystickImpl*)iface;
1292 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1294 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1295 return linuxinput_get_info_A(This->joyfd, guid, pdei);
1296 #else
1297 return DI_OK;
1298 #endif
1301 static HRESULT WINAPI JoystickWImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8W iface,
1302 LPDIEFFECTINFOW pdei,
1303 REFGUID guid)
1305 JoystickImpl* This = (JoystickImpl*)iface;
1307 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1309 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1310 return linuxinput_get_info_W(This->joyfd, guid, pdei);
1311 #else
1312 return DI_OK;
1313 #endif
1316 /*******************************************************************************
1317 * GetForceFeedbackState - Get information about the device's FF state
1319 static HRESULT WINAPI JoystickAImpl_GetForceFeedbackState(
1320 LPDIRECTINPUTDEVICE8A iface,
1321 LPDWORD pdwOut)
1323 JoystickImpl* This = (JoystickImpl*)iface;
1325 TRACE("(this=%p,%p)\n", This, pdwOut);
1327 (*pdwOut) = 0;
1329 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1330 /* DIGFFS_STOPPED is the only mandatory flag to report */
1331 if (This->ff_state == FF_STATUS_STOPPED)
1332 (*pdwOut) |= DIGFFS_STOPPED;
1333 #endif
1335 return DI_OK;
1338 /*******************************************************************************
1339 * SendForceFeedbackCommand - Send a command to the device's FF system
1341 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(
1342 LPDIRECTINPUTDEVICE8A iface,
1343 DWORD dwFlags)
1345 JoystickImpl* This = (JoystickImpl*)iface;
1346 TRACE("(this=%p,%d)\n", This, dwFlags);
1348 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1349 switch (dwFlags)
1351 case DISFFC_STOPALL:
1353 /* Stop all effects */
1354 effect_list_item *itr;
1356 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry)
1357 IDirectInputEffect_Stop(itr->ref);
1358 break;
1361 case DISFFC_RESET:
1363 effect_list_item *itr, *ptr;
1365 /* Stop, unload, release and free all effects */
1366 /* This returns the device to its "bare" state */
1367 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1368 IDirectInputEffect_Release(itr->ref);
1369 break;
1371 case DISFFC_PAUSE:
1372 case DISFFC_CONTINUE:
1373 FIXME("No support for Pause or Continue in linux\n");
1374 break;
1376 case DISFFC_SETACTUATORSOFF:
1377 case DISFFC_SETACTUATORSON:
1378 FIXME("No direct actuator control in linux\n");
1379 break;
1381 default:
1382 FIXME("Unknown Force Feedback Command!\n");
1383 return DIERR_INVALIDPARAM;
1385 return DI_OK;
1386 #else
1387 return DIERR_UNSUPPORTED;
1388 #endif
1391 /*******************************************************************************
1392 * EnumCreatedEffectObjects - Enumerate all the effects that have been
1393 * created for this device.
1395 static HRESULT WINAPI JoystickAImpl_EnumCreatedEffectObjects(
1396 LPDIRECTINPUTDEVICE8A iface,
1397 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1398 LPVOID pvRef,
1399 DWORD dwFlags)
1401 /* this function is safe to call on non-ff-enabled builds */
1402 JoystickImpl* This = (JoystickImpl*)iface;
1403 effect_list_item *itr, *ptr;
1405 TRACE("(this=%p,%p,%p,%d)\n", This, lpCallback, pvRef, dwFlags);
1407 if (!lpCallback)
1408 return DIERR_INVALIDPARAM;
1410 if (dwFlags != 0)
1411 FIXME("Flags specified, but no flags exist yet (DX9)!\n");
1413 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1414 (*lpCallback)(itr->ref, pvRef);
1416 return DI_OK;
1419 /******************************************************************************
1420 * GetDeviceInfo : get information about a device's identity
1422 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface,
1423 LPDIDEVICEINSTANCEA pdidi)
1425 JoystickImpl *This = (JoystickImpl *)iface;
1427 TRACE("(%p) %p\n", This, pdidi);
1429 if (pdidi == NULL) return E_POINTER;
1430 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1431 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)))
1432 return DIERR_INVALIDPARAM;
1434 fill_joystick_dideviceinstanceA(pdidi, This->base.dinput->dwVersion,
1435 get_joystick_index(&This->base.guid));
1436 return DI_OK;
1439 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface,
1440 LPDIDEVICEINSTANCEW pdidi)
1442 JoystickImpl *This = (JoystickImpl *)iface;
1444 TRACE("(%p) %p\n", This, pdidi);
1446 if (pdidi == NULL) return E_POINTER;
1447 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1448 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)))
1449 return DIERR_INVALIDPARAM;
1451 fill_joystick_dideviceinstanceW(pdidi, This->base.dinput->dwVersion,
1452 get_joystick_index(&This->base.guid));
1453 return DI_OK;
1456 static const IDirectInputDevice8AVtbl JoystickAvt =
1458 IDirectInputDevice2AImpl_QueryInterface,
1459 IDirectInputDevice2AImpl_AddRef,
1460 IDirectInputDevice2AImpl_Release,
1461 JoystickAImpl_GetCapabilities,
1462 IDirectInputDevice2AImpl_EnumObjects,
1463 JoystickAImpl_GetProperty,
1464 JoystickAImpl_SetProperty,
1465 JoystickAImpl_Acquire,
1466 JoystickAImpl_Unacquire,
1467 JoystickAImpl_GetDeviceState,
1468 IDirectInputDevice2AImpl_GetDeviceData,
1469 IDirectInputDevice2AImpl_SetDataFormat,
1470 IDirectInputDevice2AImpl_SetEventNotification,
1471 IDirectInputDevice2AImpl_SetCooperativeLevel,
1472 JoystickAImpl_GetObjectInfo,
1473 JoystickAImpl_GetDeviceInfo,
1474 IDirectInputDevice2AImpl_RunControlPanel,
1475 IDirectInputDevice2AImpl_Initialize,
1476 JoystickAImpl_CreateEffect,
1477 JoystickAImpl_EnumEffects,
1478 JoystickAImpl_GetEffectInfo,
1479 JoystickAImpl_GetForceFeedbackState,
1480 JoystickAImpl_SendForceFeedbackCommand,
1481 JoystickAImpl_EnumCreatedEffectObjects,
1482 IDirectInputDevice2AImpl_Escape,
1483 JoystickAImpl_Poll,
1484 IDirectInputDevice2AImpl_SendDeviceData,
1485 IDirectInputDevice7AImpl_EnumEffectsInFile,
1486 IDirectInputDevice7AImpl_WriteEffectToFile,
1487 IDirectInputDevice8AImpl_BuildActionMap,
1488 IDirectInputDevice8AImpl_SetActionMap,
1489 IDirectInputDevice8AImpl_GetImageInfo
1492 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1493 # define XCAST(fun) (typeof(JoystickWvt.fun))
1494 #else
1495 # define XCAST(fun) (void*)
1496 #endif
1498 static const IDirectInputDevice8WVtbl JoystickWvt =
1500 IDirectInputDevice2WImpl_QueryInterface,
1501 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1502 XCAST(Release)IDirectInputDevice2AImpl_Release,
1503 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1504 IDirectInputDevice2WImpl_EnumObjects,
1505 XCAST(GetProperty)JoystickAImpl_GetProperty,
1506 XCAST(SetProperty)JoystickAImpl_SetProperty,
1507 XCAST(Acquire)JoystickAImpl_Acquire,
1508 XCAST(Unacquire)JoystickAImpl_Unacquire,
1509 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1510 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1511 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1512 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1513 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1514 JoystickWImpl_GetObjectInfo,
1515 JoystickWImpl_GetDeviceInfo,
1516 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1517 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1518 XCAST(CreateEffect)JoystickAImpl_CreateEffect,
1519 JoystickWImpl_EnumEffects,
1520 JoystickWImpl_GetEffectInfo,
1521 XCAST(GetForceFeedbackState)JoystickAImpl_GetForceFeedbackState,
1522 XCAST(SendForceFeedbackCommand)JoystickAImpl_SendForceFeedbackCommand,
1523 XCAST(EnumCreatedEffectObjects)JoystickAImpl_EnumCreatedEffectObjects,
1524 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1525 XCAST(Poll)JoystickAImpl_Poll,
1526 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1527 IDirectInputDevice7WImpl_EnumEffectsInFile,
1528 IDirectInputDevice7WImpl_WriteEffectToFile,
1529 IDirectInputDevice8WImpl_BuildActionMap,
1530 IDirectInputDevice8WImpl_SetActionMap,
1531 IDirectInputDevice8WImpl_GetImageInfo
1533 #undef XCAST
1535 #else /* HAVE_CORRECT_LINUXINPUT_H */
1537 const struct dinput_device joystick_linuxinput_device = {
1538 "Wine Linux-input joystick driver",
1539 NULL,
1540 NULL,
1541 NULL,
1542 NULL
1545 #endif /* HAVE_CORRECT_LINUXINPUT_H */