notepad: Only append .txt to missing files without a dot in them.
[wine/hacks.git] / dlls / dinput / joystick_linuxinput.c
bloba7c9c634e6a7e3ef5e39af39d0b0dccb3980496e
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 (have_joydevs!=-1) {
221 return;
224 have_joydevs = 0;
226 for (i=0;i<MAX_JOYDEV;i++) {
227 char buf[MAX_PATH];
228 struct JoyDev joydev = {0};
229 int fd;
230 int no_ff_check = 0;
231 int j;
233 snprintf(buf,MAX_PATH,EVDEVPREFIX"%d",i);
234 buf[MAX_PATH-1] = 0;
236 if ((fd=open(buf, O_RDWR))==-1) {
237 fd = open(buf, O_RDONLY);
238 no_ff_check = 1;
241 if (fd!=-1) {
243 if ((-1==ioctl(fd,EVIOCGBIT(0,sizeof(joydev.evbits)),joydev.evbits))) {
244 perror("EVIOCGBIT 0");
245 close(fd);
246 continue;
248 if (-1==ioctl(fd,EVIOCGBIT(EV_ABS,sizeof(joydev.absbits)),joydev.absbits)) {
249 perror("EVIOCGBIT EV_ABS");
250 close(fd);
251 continue;
253 if (-1==ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(joydev.keybits)),joydev.keybits)) {
254 perror("EVIOCGBIT EV_KEY");
255 close(fd);
256 continue;
258 /* A true joystick has at least axis X and Y, and at least 1
259 * button. copied from linux/drivers/input/joydev.c */
260 if (test_bit(joydev.absbits,ABS_X) && test_bit(joydev.absbits,ABS_Y) &&
261 ( test_bit(joydev.keybits,BTN_TRIGGER) ||
262 test_bit(joydev.keybits,BTN_A) ||
263 test_bit(joydev.keybits,BTN_1)
267 joydev.device = strdup(buf);
269 if (-1!=ioctl(fd, EVIOCGNAME(MAX_PATH-1), buf)) {
270 buf[MAX_PATH-1] = 0;
271 joydev.name = strdup(buf);
272 } else {
273 joydev.name = joydev.device;
276 joydev.guid = DInput_Wine_Joystick_Base_GUID;
277 joydev.guid.Data3 += have_joydevs;
279 TRACE("Found a joystick on %s: %s (%s)\n",
280 joydev.device, joydev.name,
281 debugstr_guid(&joydev.guid)
284 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
285 if (!no_ff_check &&
286 test_bit(joydev.evbits, EV_FF) &&
287 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(joydev.ffbits)), joydev.ffbits) != -1 &&
288 ioctl(fd, EVIOCGEFFECTS, &joydev.num_effects) != -1 &&
289 joydev.num_effects > 0)
291 TRACE(" ... with force feedback\n");
292 joydev.has_ff = 1;
294 #endif
296 for (j=0;j<ABS_MAX;j++) {
297 if (test_bit(joydev.absbits,j)) {
298 if (-1!=ioctl(fd,EVIOCGABS(j),&(joydev.axes[j]))) {
299 TRACE(" ... with axis %d: cur=%d, min=%d, max=%d, fuzz=%d, flat=%d\n",
301 joydev.axes[j].value,
302 joydev.axes[j].minimum,
303 joydev.axes[j].maximum,
304 joydev.axes[j].fuzz,
305 joydev.axes[j].flat
311 if (have_joydevs==0) {
312 joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
313 } else {
314 HeapReAlloc(GetProcessHeap(), 0, joydevs, (1+have_joydevs) * sizeof(struct JoyDev));
316 memcpy(joydevs+have_joydevs, &joydev, sizeof(struct JoyDev));
317 have_joydevs++;
320 close(fd);
325 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
327 DWORD dwSize = lpddi->dwSize;
329 TRACE("%d %p\n", dwSize, lpddi);
330 memset(lpddi, 0, dwSize);
332 lpddi->dwSize = dwSize;
333 lpddi->guidInstance = joydevs[id].guid;
334 lpddi->guidProduct = DInput_Wine_Joystick_Base_GUID;
335 lpddi->guidFFDriver = GUID_NULL;
337 if (version >= 0x0800)
338 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
339 else
340 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
342 strcpy(lpddi->tszInstanceName, joydevs[id].name);
343 strcpy(lpddi->tszProductName, joydevs[id].device);
346 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
348 DWORD dwSize = lpddi->dwSize;
350 TRACE("%d %p\n", dwSize, lpddi);
351 memset(lpddi, 0, dwSize);
353 lpddi->dwSize = dwSize;
354 lpddi->guidInstance = joydevs[id].guid;
355 lpddi->guidProduct = DInput_Wine_Joystick_Base_GUID;
356 lpddi->guidFFDriver = GUID_NULL;
358 if (version >= 0x0800)
359 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
360 else
361 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
363 MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
364 MultiByteToWideChar(CP_ACP, 0, joydevs[id].device, -1, lpddi->tszProductName, MAX_PATH);
367 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
369 find_joydevs();
371 if (id >= have_joydevs) {
372 return FALSE;
375 if (!((dwDevType == 0) ||
376 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
377 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
378 return FALSE;
380 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
381 if (dwFlags & DIEDFL_FORCEFEEDBACK)
382 return FALSE;
383 #endif
385 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
386 fill_joystick_dideviceinstanceA(lpddi, version, id);
387 return TRUE;
389 return FALSE;
392 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
394 find_joydevs();
396 if (id >= have_joydevs) {
397 return FALSE;
400 if (!((dwDevType == 0) ||
401 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
402 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
403 return FALSE;
405 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
406 if (dwFlags & DIEDFL_FORCEFEEDBACK)
407 return FALSE;
408 #endif
410 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
411 fill_joystick_dideviceinstanceW(lpddi, version, id);
412 return TRUE;
414 return FALSE;
417 static JoystickImpl *alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput, unsigned short index)
419 JoystickImpl* newDevice;
420 LPDIDATAFORMAT df = NULL;
421 int i, idx = 0;
422 char buffer[MAX_PATH+16];
423 HKEY hkey, appkey;
424 LONG def_deadzone = 0;
426 newDevice = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(JoystickImpl));
427 if (!newDevice) return NULL;
429 newDevice->base.lpVtbl = jvt;
430 newDevice->base.ref = 1;
431 newDevice->base.guid = *rguid;
432 newDevice->base.dinput = dinput;
433 newDevice->joyfd = -1;
434 newDevice->joydev = &joydevs[index];
435 list_init(&newDevice->ff_effects);
436 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
437 newDevice->ff_state = FF_STATUS_STOPPED;
438 #endif
439 InitializeCriticalSection(&newDevice->base.crit);
440 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
442 /* get options */
443 get_app_key(&hkey, &appkey);
445 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH))
447 def_deadzone = atoi(buffer);
448 TRACE("setting default deadzone to: %d\n", def_deadzone);
450 if (appkey) RegCloseKey(appkey);
451 if (hkey) RegCloseKey(hkey);
453 /* Create copy of default data format */
454 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto failed;
455 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
456 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
458 /* Supported Axis & POVs should map 1-to-1 */
459 for (i = 0; i < WINE_JOYSTICK_MAX_AXES; i++)
461 if (!test_bit(newDevice->joydev->absbits, i)) {
462 newDevice->axes[i] = -1;
463 continue;
466 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i], df->dwObjSize);
467 newDevice->axes[i] = idx;
468 newDevice->props[idx].lDevMin = newDevice->joydev->axes[i].minimum;
469 newDevice->props[idx].lDevMax = newDevice->joydev->axes[i].maximum;
470 newDevice->props[idx].lMin = 0;
471 newDevice->props[idx].lMax = 0xffff;
472 newDevice->props[idx].lSaturation = 0;
473 newDevice->props[idx].lDeadZone = def_deadzone;
475 /* Linux supports force-feedback on X & Y axes only */
476 if (newDevice->joydev->has_ff && (i == 0 || i == 1))
477 df->rgodf[idx].dwFlags |= DIDOI_FFACTUATOR;
479 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->numAxes++) | DIDFT_ABSAXIS;
482 for (i = 0; i < WINE_JOYSTICK_MAX_POVS; i++)
484 if (!test_bit(newDevice->joydev->absbits, ABS_HAT0X + i * 2) ||
485 !test_bit(newDevice->joydev->absbits, ABS_HAT0Y + i * 2)) {
486 newDevice->axes[ABS_HAT0X + i * 2] = newDevice->axes[ABS_HAT0Y + i * 2] = -1;
487 continue;
490 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + WINE_JOYSTICK_MAX_AXES], df->dwObjSize);
491 newDevice->axes[ABS_HAT0X + i * 2] = newDevice->axes[ABS_HAT0Y + i * 2] = i;
492 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->numPOVs++) | DIDFT_POV;
495 /* Buttons can be anywhere, so check all */
496 for (i = 0; i < KEY_MAX && newDevice->numButtons < WINE_JOYSTICK_MAX_BUTTONS; i++)
498 if (!test_bit(newDevice->joydev->keybits, i)) continue;
500 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[newDevice->numButtons + WINE_JOYSTICK_MAX_AXES + WINE_JOYSTICK_MAX_POVS], df->dwObjSize);
501 newDevice->buttons[i] = 0x80 | newDevice->numButtons;
502 df->rgodf[idx ].pguid = &GUID_Button;
503 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->numButtons++) | DIDFT_PSHBUTTON;
505 df->dwNumObjs = idx;
507 fake_current_js_state(newDevice);
509 newDevice->base.data_format.wine_df = df;
510 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
511 return newDevice;
513 failed:
514 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
515 HeapFree(GetProcessHeap(), 0, df);
516 HeapFree(GetProcessHeap(), 0, newDevice);
517 return NULL;
520 /******************************************************************************
521 * get_joystick_index : Get the joystick index from a given GUID
523 static unsigned short get_joystick_index(REFGUID guid)
525 GUID wine_joystick = DInput_Wine_Joystick_Base_GUID;
526 GUID dev_guid = *guid;
528 wine_joystick.Data3 = 0;
529 dev_guid.Data3 = 0;
531 /* for the standard joystick GUID use index 0 */
532 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
534 /* for the wine joystick GUIDs use the index stored in Data3 */
535 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3 - DInput_Wine_Joystick_Base_GUID.Data3;
537 return MAX_JOYDEV;
540 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
542 unsigned short index;
544 find_joydevs();
546 if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
547 have_joydevs && index < have_joydevs)
549 if ((riid == NULL) ||
550 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
551 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
552 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
553 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
555 *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &JoystickAvt, dinput, index);
556 TRACE("Created a Joystick device (%p)\n", *pdev);
558 if (*pdev == NULL)
560 ERR("out of memory\n");
561 return DIERR_OUTOFMEMORY;
563 return DI_OK;
566 WARN("no interface\n");
567 return DIERR_NOINTERFACE;
570 return DIERR_DEVICENOTREG;
574 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
576 unsigned short index;
578 find_joydevs();
580 if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
581 have_joydevs && index < have_joydevs)
583 if ((riid == NULL) ||
584 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
585 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
586 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
587 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
589 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &JoystickWvt, dinput, index);
590 TRACE("Created a Joystick device (%p)\n", *pdev);
592 if (*pdev == NULL)
594 ERR("out of memory\n");
595 return DIERR_OUTOFMEMORY;
597 return DI_OK;
599 WARN("no interface\n");
600 return DIERR_NOINTERFACE;
603 WARN("invalid device GUID\n");
604 return DIERR_DEVICENOTREG;
607 const struct dinput_device joystick_linuxinput_device = {
608 "Wine Linux-input joystick driver",
609 joydev_enum_deviceA,
610 joydev_enum_deviceW,
611 joydev_create_deviceA,
612 joydev_create_deviceW
615 /******************************************************************************
616 * Acquire : gets exclusive control of the joystick
618 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
620 JoystickImpl *This = (JoystickImpl *)iface;
621 HRESULT res;
623 TRACE("(this=%p)\n",This);
625 res = IDirectInputDevice2AImpl_Acquire(iface);
626 if (res==DI_OK) {
627 if (-1==(This->joyfd=open(This->joydev->device,O_RDWR))) {
628 if (-1==(This->joyfd=open(This->joydev->device,O_RDONLY))) {
629 /* Couldn't open the device at all */
630 perror(This->joydev->device);
631 IDirectInputDevice2AImpl_Unacquire(iface);
632 return DIERR_NOTFOUND;
633 } else {
634 /* Couldn't open in r/w but opened in read-only. */
635 WARN("Could not open %s in read-write mode. Force feedback will be disabled.\n", This->joydev->device);
639 else
640 WARN("Failed to acquire: %x\n", res);
642 return res;
645 /******************************************************************************
646 * Unacquire : frees the joystick
648 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
650 JoystickImpl *This = (JoystickImpl *)iface;
651 HRESULT res;
653 TRACE("(this=%p)\n",This);
654 res = IDirectInputDevice2AImpl_Unacquire(iface);
655 if (res==DI_OK && This->joyfd!=-1) {
656 close(This->joyfd);
657 This->joyfd = -1;
659 return res;
663 * set the current state of the js device as it would be with the middle
664 * values on the axes
666 #define CENTER_AXIS(a) \
667 (ji->axes[a] == -1 ? 0 : joystick_map_axis( &ji->props[ji->axes[a]], \
668 ji->joydev->axes[a].value ))
669 static void fake_current_js_state(JoystickImpl *ji)
671 int i;
673 /* center the axes */
674 ji->js.lX = CENTER_AXIS(ABS_X);
675 ji->js.lY = CENTER_AXIS(ABS_Y);
676 ji->js.lZ = CENTER_AXIS(ABS_Z);
677 ji->js.lRx = CENTER_AXIS(ABS_RX);
678 ji->js.lRy = CENTER_AXIS(ABS_RY);
679 ji->js.lRz = CENTER_AXIS(ABS_RZ);
680 ji->js.rglSlider[0] = CENTER_AXIS(ABS_THROTTLE);
681 ji->js.rglSlider[1] = CENTER_AXIS(ABS_RUDDER);
683 /* POV center is -1 */
684 for (i = 0; i < 4; i++)
685 ji->js.rgdwPOV[i] = -1;
687 #undef CENTER_AXIS
689 /* convert wine format offset to user format object index */
690 static void joy_polldev(JoystickImpl *This)
692 struct pollfd plfd;
693 struct input_event ie;
695 if (This->joyfd==-1)
696 return;
698 while (1)
700 LONG value = 0;
701 int inst_id = -1;
703 plfd.fd = This->joyfd;
704 plfd.events = POLLIN;
706 if (poll(&plfd,1,0) != 1)
707 return;
709 /* we have one event, so we can read */
710 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
711 return;
713 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
714 switch (ie.type) {
715 case EV_KEY: /* button */
717 int btn = This->buttons[ie.code];
719 TRACE("(%p) %d -> %d\n", This, ie.code, btn);
720 if (btn & 0x80)
722 btn &= 0x7F;
723 inst_id = DIDFT_MAKEINSTANCE(btn) | DIDFT_PSHBUTTON;
724 This->js.rgbButtons[btn] = value = ie.value ? 0x80 : 0x00;
726 break;
728 case EV_ABS:
730 int axis = This->axes[ie.code];
731 if (axis==-1) {
732 break;
734 inst_id = DIDFT_MAKEINSTANCE(axis) | (ie.code < ABS_HAT0X ? DIDFT_ABSAXIS : DIDFT_POV);
735 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], ie.value);
737 switch (ie.code) {
738 case ABS_X: This->js.lX = value; break;
739 case ABS_Y: This->js.lY = value; break;
740 case ABS_Z: This->js.lZ = value; break;
741 case ABS_RX: This->js.lRx = value; break;
742 case ABS_RY: This->js.lRy = value; break;
743 case ABS_RZ: This->js.lRz = value; break;
744 case ABS_THROTTLE: This->js.rglSlider[0] = value; break;
745 case ABS_RUDDER: This->js.rglSlider[1] = value; break;
746 case ABS_HAT0X: case ABS_HAT0Y: case ABS_HAT1X: case ABS_HAT1Y:
747 case ABS_HAT2X: case ABS_HAT2Y: case ABS_HAT3X: case ABS_HAT3Y:
749 int idx = (ie.code - ABS_HAT0X) / 2;
751 if (ie.code % 2)
752 This->povs[idx].y = ie.value;
753 else
754 This->povs[idx].x = ie.value;
756 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
757 break;
759 default:
760 FIXME("unhandled joystick axis event (code %d, value %d)\n",ie.code,ie.value);
762 break;
764 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
765 case EV_FF_STATUS:
766 This->ff_state = ie.value;
767 break;
768 #endif
769 #ifdef EV_SYN
770 case EV_SYN:
771 /* there is nothing to do */
772 break;
773 #endif
774 default:
775 FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
776 break;
778 if (inst_id >= 0)
779 queue_event((LPDIRECTINPUTDEVICE8A)This,
780 id_to_offset(&This->base.data_format, inst_id),
781 value, ie.time.tv_usec, This->base.dinput->evsequence++);
785 /******************************************************************************
786 * GetDeviceState : returns the "state" of the joystick.
789 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
790 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
792 JoystickImpl *This = (JoystickImpl *)iface;
794 TRACE("(this=%p,0x%08x,%p)\n", This, len, ptr);
796 if (!This->base.acquired)
798 WARN("not acquired\n");
799 return DIERR_NOTACQUIRED;
802 joy_polldev(This);
804 /* convert and copy data to user supplied buffer */
805 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
807 return DI_OK;
810 /******************************************************************************
811 * SetProperty : change input device properties
813 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface,
814 REFGUID rguid,
815 LPCDIPROPHEADER ph)
817 JoystickImpl *This = (JoystickImpl *)iface;
819 if (!ph) {
820 WARN("invalid argument\n");
821 return DIERR_INVALIDPARAM;
824 TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
825 TRACE("ph.dwSize = %d, ph.dwHeaderSize =%d, ph.dwObj = %d, ph.dwHow= %d\n",
826 ph->dwSize, ph->dwHeaderSize, ph->dwObj, ph->dwHow);
828 if (!HIWORD(rguid)) {
829 switch (LOWORD(rguid)) {
830 case (DWORD)DIPROP_RANGE: {
831 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
833 if (ph->dwHow == DIPH_DEVICE) {
834 int i;
835 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
836 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
837 /* Scale dead-zone */
838 This->props[i].lDeadZone = MulDiv(This->props[i].lDeadZone, pr->lMax - pr->lMin,
839 This->props[i].lMax - This->props[i].lMin);
840 This->props[i].lMin = pr->lMin;
841 This->props[i].lMax = pr->lMax;
843 } else {
844 int obj = find_property(&This->base.data_format, ph);
846 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
847 if (obj >= 0) {
848 /* Scale dead-zone */
849 This->props[obj].lDeadZone = MulDiv(This->props[obj].lDeadZone, pr->lMax - pr->lMin,
850 This->props[obj].lMax - This->props[obj].lMin);
851 This->props[obj].lMin = pr->lMin;
852 This->props[obj].lMax = pr->lMax;
855 fake_current_js_state(This);
856 break;
858 case (DWORD)DIPROP_DEADZONE: {
859 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
860 if (ph->dwHow == DIPH_DEVICE) {
861 int i;
862 TRACE("deadzone(%d) all\n", pd->dwData);
863 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
864 This->props[i].lDeadZone = pd->dwData;
866 } else {
867 int obj = find_property(&This->base.data_format, ph);
869 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
870 if (obj >= 0) {
871 This->props[obj].lDeadZone = pd->dwData;
874 fake_current_js_state(This);
875 break;
877 case (DWORD)DIPROP_CALIBRATIONMODE: {
878 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
879 FIXME("DIPROP_CALIBRATIONMODE(%d)\n", pd->dwData);
880 break;
882 case (DWORD)DIPROP_AUTOCENTER: {
883 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
885 FIXME("DIPROP_AUTOCENTER(%d)\n", pd->dwData);
886 break;
888 case (DWORD)DIPROP_SATURATION: {
889 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
891 if (ph->dwHow == DIPH_DEVICE) {
892 int i;
894 TRACE("saturation(%d) all\n", pd->dwData);
895 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
896 This->props[i].lSaturation = pd->dwData;
897 } else {
898 int obj = find_property(&This->base.data_format, ph);
900 if (obj < 0) return DIERR_OBJECTNOTFOUND;
902 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
903 This->props[obj].lSaturation = pd->dwData;
905 fake_current_js_state(This);
906 break;
908 default:
909 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
912 return DI_OK;
915 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
916 LPDIRECTINPUTDEVICE8A iface,
917 LPDIDEVCAPS lpDIDevCaps)
919 JoystickImpl *This = (JoystickImpl *)iface;
921 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
923 if (!lpDIDevCaps) {
924 WARN("invalid pointer\n");
925 return E_POINTER;
928 if (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) {
929 WARN("invalid argument\n");
930 return DIERR_INVALIDPARAM;
933 lpDIDevCaps->dwFlags = DIDC_ATTACHED;
934 if (This->base.dinput->dwVersion >= 0x0800)
935 lpDIDevCaps->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
936 else
937 lpDIDevCaps->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
939 if (This->joydev->has_ff)
940 lpDIDevCaps->dwFlags |= DIDC_FORCEFEEDBACK;
942 lpDIDevCaps->dwAxes = This->numAxes;
943 lpDIDevCaps->dwButtons = This->numButtons;
944 lpDIDevCaps->dwPOVs = This->numPOVs;
946 return DI_OK;
949 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
951 JoystickImpl *This = (JoystickImpl *)iface;
953 TRACE("(%p)\n",This);
955 if (!This->base.acquired)
956 return DIERR_NOTACQUIRED;
958 joy_polldev(This);
959 return DI_OK;
962 /******************************************************************************
963 * GetProperty : get input device properties
965 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
966 REFGUID rguid,
967 LPDIPROPHEADER pdiph)
969 JoystickImpl *This = (JoystickImpl *)iface;
971 TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
972 _dump_DIPROPHEADER(pdiph);
974 if (HIWORD(rguid)) return DI_OK;
976 switch (LOWORD(rguid)) {
977 case (DWORD) DIPROP_RANGE:
979 LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
980 int obj = find_property(&This->base.data_format, pdiph);
982 if (obj < 0) return DIERR_OBJECTNOTFOUND;
984 pr->lMin = This->props[obj].lMin;
985 pr->lMax = This->props[obj].lMax;
986 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
987 break;
989 case (DWORD) DIPROP_DEADZONE:
991 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
992 int obj = find_property(&This->base.data_format, pdiph);
994 if (obj < 0) return DIERR_OBJECTNOTFOUND;
996 pd->dwData = This->props[obj].lDeadZone;
997 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
998 break;
1000 case (DWORD) DIPROP_SATURATION:
1002 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1003 int obj = find_property(&This->base.data_format, pdiph);
1005 if (obj < 0) return DIERR_OBJECTNOTFOUND;
1007 pd->dwData = This->props[obj].lSaturation;
1008 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
1009 break;
1012 default:
1013 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
1016 return DI_OK;
1019 /******************************************************************************
1020 * GetObjectInfo : get information about a device object such as a button
1021 * or axis
1023 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
1024 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
1026 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
1027 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
1028 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
1029 HRESULT res;
1031 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
1032 if (res != DI_OK) return res;
1034 if (pdidoi->dwType & DIDFT_AXIS)
1035 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
1036 else if (pdidoi->dwType & DIDFT_POV)
1037 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
1038 else if (pdidoi->dwType & DIDFT_BUTTON)
1039 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
1041 _dump_OBJECTINSTANCEW(pdidoi);
1042 return res;
1045 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
1046 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
1048 HRESULT res;
1049 DIDEVICEOBJECTINSTANCEW didoiW;
1050 DWORD dwSize = pdidoi->dwSize;
1052 didoiW.dwSize = sizeof(didoiW);
1053 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
1054 if (res != DI_OK) return res;
1056 memset(pdidoi, 0, pdidoi->dwSize);
1057 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
1058 pdidoi->dwSize = dwSize;
1059 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
1060 sizeof(pdidoi->tszName), NULL, NULL);
1062 return res;
1065 /******************************************************************************
1066 * CreateEffect - Create a new FF effect with the specified params
1068 static HRESULT WINAPI JoystickAImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface,
1069 REFGUID rguid,
1070 LPCDIEFFECT lpeff,
1071 LPDIRECTINPUTEFFECT *ppdef,
1072 LPUNKNOWN pUnkOuter)
1074 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1075 effect_list_item* new_effect = NULL;
1076 HRESULT retval = DI_OK;
1077 #endif
1079 JoystickImpl* This = (JoystickImpl*)iface;
1080 TRACE("(this=%p,%p,%p,%p,%p)\n", This, rguid, lpeff, ppdef, pUnkOuter);
1082 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
1083 TRACE("not available (compiled w/o ff support)\n");
1084 *ppdef = NULL;
1085 return DI_OK;
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, 0);
1102 if (retval != DI_OK && retval != DI_DOWNLOADSKIPPED)
1104 HeapFree(GetProcessHeap(), 0, new_effect);
1105 return retval;
1109 list_add_tail(&This->ff_effects, &new_effect->entry);
1110 *ppdef = new_effect->ref;
1112 if (pUnkOuter != NULL)
1113 FIXME("Interface aggregation not implemented.\n");
1115 return DI_OK;
1117 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */
1120 /*******************************************************************************
1121 * EnumEffects - Enumerate available FF effects
1123 static HRESULT WINAPI JoystickAImpl_EnumEffects(LPDIRECTINPUTDEVICE8A iface,
1124 LPDIENUMEFFECTSCALLBACKA lpCallback,
1125 LPVOID pvRef,
1126 DWORD dwEffType)
1128 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1129 DIEFFECTINFOA dei; /* feif */
1130 DWORD type = DIEFT_GETTYPE(dwEffType);
1131 JoystickImpl* This = (JoystickImpl*)iface;
1133 TRACE("(this=%p,%p,%d) type=%d\n", This, pvRef, dwEffType, type);
1135 dei.dwSize = sizeof(DIEFFECTINFOA);
1137 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1138 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1139 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1140 (*lpCallback)(&dei, pvRef);
1143 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1144 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1145 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1146 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1147 (*lpCallback)(&dei, pvRef);
1149 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1150 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1151 (*lpCallback)(&dei, pvRef);
1153 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1154 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1155 (*lpCallback)(&dei, pvRef);
1157 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1158 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1159 (*lpCallback)(&dei, pvRef);
1161 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1162 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1163 (*lpCallback)(&dei, pvRef);
1167 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1168 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1169 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1170 (*lpCallback)(&dei, pvRef);
1173 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1174 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1175 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1176 (*lpCallback)(&dei, pvRef);
1178 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1179 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1180 (*lpCallback)(&dei, pvRef);
1182 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1183 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1184 (*lpCallback)(&dei, pvRef);
1186 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1187 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1188 (*lpCallback)(&dei, pvRef);
1192 #endif
1194 return DI_OK;
1197 static HRESULT WINAPI JoystickWImpl_EnumEffects(LPDIRECTINPUTDEVICE8W iface,
1198 LPDIENUMEFFECTSCALLBACKW lpCallback,
1199 LPVOID pvRef,
1200 DWORD dwEffType)
1202 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1203 /* seems silly to duplicate all this code but all the structures and functions
1204 * are actually different (A/W) */
1205 DIEFFECTINFOW dei; /* feif */
1206 DWORD type = DIEFT_GETTYPE(dwEffType);
1207 JoystickImpl* This = (JoystickImpl*)iface;
1208 int xfd = This->joyfd;
1210 TRACE("(this=%p,%p,%d) type=%d fd=%d\n", This, pvRef, dwEffType, type, xfd);
1212 dei.dwSize = sizeof(DIEFFECTINFOW);
1214 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1215 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1216 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1217 (*lpCallback)(&dei, pvRef);
1220 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1221 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1222 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1223 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1224 (*lpCallback)(&dei, pvRef);
1226 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1227 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1228 (*lpCallback)(&dei, pvRef);
1230 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1231 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1232 (*lpCallback)(&dei, pvRef);
1234 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1235 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1236 (*lpCallback)(&dei, pvRef);
1238 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1239 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1240 (*lpCallback)(&dei, pvRef);
1244 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1245 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1246 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1247 (*lpCallback)(&dei, pvRef);
1250 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1251 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1252 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1253 (*lpCallback)(&dei, pvRef);
1255 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1256 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1257 (*lpCallback)(&dei, pvRef);
1259 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1260 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1261 (*lpCallback)(&dei, pvRef);
1263 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1264 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1265 (*lpCallback)(&dei, pvRef);
1269 /* return to unacquired state if that's where it was */
1270 if (xfd == -1)
1271 IDirectInputDevice8_Unacquire(iface);
1272 #endif
1274 return DI_OK;
1277 /*******************************************************************************
1278 * GetEffectInfo - Get information about a particular effect
1280 static HRESULT WINAPI JoystickAImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8A iface,
1281 LPDIEFFECTINFOA pdei,
1282 REFGUID guid)
1284 JoystickImpl* This = (JoystickImpl*)iface;
1286 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1288 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1289 return linuxinput_get_info_A(This->joyfd, guid, pdei);
1290 #else
1291 return DI_OK;
1292 #endif
1295 static HRESULT WINAPI JoystickWImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8W iface,
1296 LPDIEFFECTINFOW pdei,
1297 REFGUID guid)
1299 JoystickImpl* This = (JoystickImpl*)iface;
1301 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1303 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1304 return linuxinput_get_info_W(This->joyfd, guid, pdei);
1305 #else
1306 return DI_OK;
1307 #endif
1310 /*******************************************************************************
1311 * GetForceFeedbackState - Get information about the device's FF state
1313 static HRESULT WINAPI JoystickAImpl_GetForceFeedbackState(
1314 LPDIRECTINPUTDEVICE8A iface,
1315 LPDWORD pdwOut)
1317 JoystickImpl* This = (JoystickImpl*)iface;
1319 TRACE("(this=%p,%p)\n", This, pdwOut);
1321 (*pdwOut) = 0;
1323 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1324 /* DIGFFS_STOPPED is the only mandatory flag to report */
1325 if (This->ff_state == FF_STATUS_STOPPED)
1326 (*pdwOut) |= DIGFFS_STOPPED;
1327 #endif
1329 return DI_OK;
1332 /*******************************************************************************
1333 * SendForceFeedbackCommand - Send a command to the device's FF system
1335 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(
1336 LPDIRECTINPUTDEVICE8A iface,
1337 DWORD dwFlags)
1339 JoystickImpl* This = (JoystickImpl*)iface;
1340 TRACE("(this=%p,%d)\n", This, dwFlags);
1342 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1343 switch (dwFlags)
1345 case DISFFC_STOPALL:
1347 /* Stop all effects */
1348 effect_list_item *itr;
1350 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry)
1351 IDirectInputEffect_Stop(itr->ref);
1352 break;
1355 case DISFFC_RESET:
1357 effect_list_item *itr, *ptr;
1359 /* Stop, unload, release and free all effects */
1360 /* This returns the device to its "bare" state */
1361 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1362 IDirectInputEffect_Release(itr->ref);
1363 break;
1365 case DISFFC_PAUSE:
1366 case DISFFC_CONTINUE:
1367 FIXME("No support for Pause or Continue in linux\n");
1368 break;
1370 case DISFFC_SETACTUATORSOFF:
1371 case DISFFC_SETACTUATORSON:
1372 FIXME("No direct actuator control in linux\n");
1373 break;
1375 default:
1376 FIXME("Unknown Force Feedback Command!\n");
1377 return DIERR_INVALIDPARAM;
1379 return DI_OK;
1380 #else
1381 return DIERR_UNSUPPORTED;
1382 #endif
1385 /*******************************************************************************
1386 * EnumCreatedEffectObjects - Enumerate all the effects that have been
1387 * created for this device.
1389 static HRESULT WINAPI JoystickAImpl_EnumCreatedEffectObjects(
1390 LPDIRECTINPUTDEVICE8A iface,
1391 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1392 LPVOID pvRef,
1393 DWORD dwFlags)
1395 /* this function is safe to call on non-ff-enabled builds */
1396 JoystickImpl* This = (JoystickImpl*)iface;
1397 effect_list_item *itr, *ptr;
1399 TRACE("(this=%p,%p,%p,%d)\n", This, lpCallback, pvRef, dwFlags);
1401 if (!lpCallback)
1402 return DIERR_INVALIDPARAM;
1404 if (dwFlags != 0)
1405 FIXME("Flags specified, but no flags exist yet (DX9)!\n");
1407 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1408 (*lpCallback)(itr->ref, pvRef);
1410 return DI_OK;
1413 /******************************************************************************
1414 * GetDeviceInfo : get information about a device's identity
1416 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface,
1417 LPDIDEVICEINSTANCEA pdidi)
1419 JoystickImpl *This = (JoystickImpl *)iface;
1421 TRACE("(%p) %p\n", This, pdidi);
1423 if (pdidi == NULL) return E_POINTER;
1424 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1425 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)))
1426 return DIERR_INVALIDPARAM;
1428 fill_joystick_dideviceinstanceA(pdidi, This->base.dinput->dwVersion,
1429 get_joystick_index(&This->base.guid));
1430 return DI_OK;
1433 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface,
1434 LPDIDEVICEINSTANCEW pdidi)
1436 JoystickImpl *This = (JoystickImpl *)iface;
1438 TRACE("(%p) %p\n", This, pdidi);
1440 if (pdidi == NULL) return E_POINTER;
1441 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1442 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)))
1443 return DIERR_INVALIDPARAM;
1445 fill_joystick_dideviceinstanceW(pdidi, This->base.dinput->dwVersion,
1446 get_joystick_index(&This->base.guid));
1447 return DI_OK;
1450 static const IDirectInputDevice8AVtbl JoystickAvt =
1452 IDirectInputDevice2AImpl_QueryInterface,
1453 IDirectInputDevice2AImpl_AddRef,
1454 IDirectInputDevice2AImpl_Release,
1455 JoystickAImpl_GetCapabilities,
1456 IDirectInputDevice2AImpl_EnumObjects,
1457 JoystickAImpl_GetProperty,
1458 JoystickAImpl_SetProperty,
1459 JoystickAImpl_Acquire,
1460 JoystickAImpl_Unacquire,
1461 JoystickAImpl_GetDeviceState,
1462 IDirectInputDevice2AImpl_GetDeviceData,
1463 IDirectInputDevice2AImpl_SetDataFormat,
1464 IDirectInputDevice2AImpl_SetEventNotification,
1465 IDirectInputDevice2AImpl_SetCooperativeLevel,
1466 JoystickAImpl_GetObjectInfo,
1467 JoystickAImpl_GetDeviceInfo,
1468 IDirectInputDevice2AImpl_RunControlPanel,
1469 IDirectInputDevice2AImpl_Initialize,
1470 JoystickAImpl_CreateEffect,
1471 JoystickAImpl_EnumEffects,
1472 JoystickAImpl_GetEffectInfo,
1473 JoystickAImpl_GetForceFeedbackState,
1474 JoystickAImpl_SendForceFeedbackCommand,
1475 JoystickAImpl_EnumCreatedEffectObjects,
1476 IDirectInputDevice2AImpl_Escape,
1477 JoystickAImpl_Poll,
1478 IDirectInputDevice2AImpl_SendDeviceData,
1479 IDirectInputDevice7AImpl_EnumEffectsInFile,
1480 IDirectInputDevice7AImpl_WriteEffectToFile,
1481 IDirectInputDevice8AImpl_BuildActionMap,
1482 IDirectInputDevice8AImpl_SetActionMap,
1483 IDirectInputDevice8AImpl_GetImageInfo
1486 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1487 # define XCAST(fun) (typeof(JoystickWvt.fun))
1488 #else
1489 # define XCAST(fun) (void*)
1490 #endif
1492 static const IDirectInputDevice8WVtbl JoystickWvt =
1494 IDirectInputDevice2WImpl_QueryInterface,
1495 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1496 XCAST(Release)IDirectInputDevice2AImpl_Release,
1497 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1498 IDirectInputDevice2WImpl_EnumObjects,
1499 XCAST(GetProperty)JoystickAImpl_GetProperty,
1500 XCAST(SetProperty)JoystickAImpl_SetProperty,
1501 XCAST(Acquire)JoystickAImpl_Acquire,
1502 XCAST(Unacquire)JoystickAImpl_Unacquire,
1503 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1504 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1505 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1506 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1507 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1508 JoystickWImpl_GetObjectInfo,
1509 JoystickWImpl_GetDeviceInfo,
1510 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1511 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1512 XCAST(CreateEffect)JoystickAImpl_CreateEffect,
1513 JoystickWImpl_EnumEffects,
1514 JoystickWImpl_GetEffectInfo,
1515 XCAST(GetForceFeedbackState)JoystickAImpl_GetForceFeedbackState,
1516 XCAST(SendForceFeedbackCommand)JoystickAImpl_SendForceFeedbackCommand,
1517 XCAST(EnumCreatedEffectObjects)JoystickAImpl_EnumCreatedEffectObjects,
1518 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1519 XCAST(Poll)JoystickAImpl_Poll,
1520 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1521 IDirectInputDevice7WImpl_EnumEffectsInFile,
1522 IDirectInputDevice7WImpl_WriteEffectToFile,
1523 IDirectInputDevice8WImpl_BuildActionMap,
1524 IDirectInputDevice8WImpl_SetActionMap,
1525 IDirectInputDevice8WImpl_GetImageInfo
1527 #undef XCAST
1529 #else /* HAVE_CORRECT_LINUXINPUT_H */
1531 const struct dinput_device joystick_linuxinput_device = {
1532 "Wine Linux-input joystick driver",
1533 NULL,
1534 NULL,
1535 NULL,
1536 NULL
1539 #endif /* HAVE_CORRECT_LINUXINPUT_H */