[DIRECTX/WINE]
[reactos.git] / reactos / dll / directx / wine / dinput / joystick_linuxinput.c
1 /* DirectInput Joystick device
2 *
3 * Copyright 1998,2000 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2005 Daniel Remenak
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include <config.h>
24 //#include "wine/port.h"
25
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
52
53 #include <wine/debug.h>
54 //#include "wine/unicode.h"
55 //#include "wine/list.h"
56 //#include "windef.h"
57 //#include "winbase.h"
58 //#include "winerror.h"
59 //#include "winreg.h"
60 //#include "dinput.h"
61
62 //#include "dinput_private.h"
63 #include "device_private.h"
64 //#include "joystick_private.h"
65
66 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
67
68 #ifdef HAS_PROPER_HEADER
69
70 #define EVDEVPREFIX "/dev/input/event"
71 #define EVDEVDRIVER " (event)"
72
73 /* Wine joystick driver object instances */
74 #define WINE_JOYSTICK_MAX_AXES 8
75 #define WINE_JOYSTICK_MAX_POVS 4
76 #define WINE_JOYSTICK_MAX_BUTTONS 128
77
78 struct wine_input_absinfo {
79 LONG value;
80 LONG minimum;
81 LONG maximum;
82 LONG fuzz;
83 LONG flat;
84 };
85
86 /* implemented in effect_linuxinput.c */
87 HRESULT linuxinput_create_effect(int* fd, REFGUID rguid, struct list *parent_list_entry, LPDIRECTINPUTEFFECT* peff);
88 HRESULT linuxinput_get_info_A(int fd, REFGUID rguid, LPDIEFFECTINFOA info);
89 HRESULT linuxinput_get_info_W(int fd, REFGUID rguid, LPDIEFFECTINFOW info);
90
91 typedef struct JoystickImpl JoystickImpl;
92 static const IDirectInputDevice8AVtbl JoystickAvt;
93 static const IDirectInputDevice8WVtbl JoystickWvt;
94
95 struct JoyDev {
96 char *device;
97 char *name;
98 GUID guid;
99
100 int has_ff;
101 int num_effects;
102
103 /* data returned by EVIOCGBIT for caps, EV_ABS, EV_KEY, and EV_FF */
104 BYTE evbits[(EV_MAX+7)/8];
105 BYTE absbits[(ABS_MAX+7)/8];
106 BYTE keybits[(KEY_MAX+7)/8];
107 BYTE ffbits[(FF_MAX+7)/8];
108
109 /* data returned by the EVIOCGABS() ioctl */
110 struct wine_input_absinfo axes[ABS_MAX];
111
112 WORD vendor_id, product_id;
113 };
114
115 struct JoystickImpl
116 {
117 struct JoystickGenericImpl generic;
118 struct JoyDev *joydev;
119
120 /* joystick private */
121 int joyfd;
122
123 int dev_axes_to_di[ABS_MAX];
124 POINTL povs[4];
125
126 /* LUT for KEY_ to offset in rgbButtons */
127 BYTE buttons[KEY_MAX];
128
129 /* Force feedback variables */
130 struct list ff_effects;
131 int ff_state;
132 int ff_autocenter;
133 int ff_gain;
134 };
135
136 static inline JoystickImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
137 {
138 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface),
139 JoystickGenericImpl, base), JoystickImpl, generic);
140 }
141 static inline JoystickImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
142 {
143 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface),
144 JoystickGenericImpl, base), JoystickImpl, generic);
145 }
146 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(JoystickImpl *This)
147 {
148 return &This->generic.base.IDirectInputDevice8A_iface;
149 }
150 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickImpl *This)
151 {
152 return &This->generic.base.IDirectInputDevice8W_iface;
153 }
154
155 static void fake_current_js_state(JoystickImpl *ji);
156 static void find_joydevs(void);
157 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface);
158
159 /* This GUID is slightly different from the linux joystick one. Take note. */
160 static const GUID DInput_Wine_Joystick_Base_GUID = { /* 9e573eda-7734-11d2-8d4a-23903fb6bdf7 */
161 0x9e573eda,
162 0x7734,
163 0x11d2,
164 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
165 };
166
167 #define test_bit(arr,bit) (((BYTE*)(arr))[(bit)>>3]&(1<<((bit)&7)))
168
169 #define MAX_JOYDEV 64
170
171 static int have_joydevs = -1;
172 static struct JoyDev *joydevs = NULL;
173
174 static void find_joydevs(void)
175 {
176 int i;
177
178 if (InterlockedCompareExchange(&have_joydevs, 0, -1) != -1)
179 /* Someone beat us to it */
180 return;
181
182 for (i = 0; i < MAX_JOYDEV; i++)
183 {
184 char buf[MAX_PATH];
185 struct JoyDev joydev = {0};
186 int fd;
187 int no_ff_check = 0;
188 int j;
189 struct JoyDev *new_joydevs;
190 struct input_id device_id = {0};
191
192 snprintf(buf, sizeof(buf), EVDEVPREFIX"%d", i);
193
194 if ((fd = open(buf, O_RDWR)) == -1)
195 {
196 fd = open(buf, O_RDONLY);
197 no_ff_check = 1;
198 }
199
200 if (fd == -1)
201 {
202 WARN("Failed to open \"%s\": %d %s\n", buf, errno, strerror(errno));
203 continue;
204 }
205
206 if (ioctl(fd, EVIOCGBIT(0, sizeof(joydev.evbits)), joydev.evbits) == -1)
207 {
208 WARN("ioct(EVIOCGBIT, 0) failed: %d %s\n", errno, strerror(errno));
209 close(fd);
210 continue;
211 }
212 if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(joydev.absbits)), joydev.absbits) == -1)
213 {
214 WARN("ioct(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno, strerror(errno));
215 close(fd);
216 continue;
217 }
218 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(joydev.keybits)), joydev.keybits) == -1)
219 {
220 WARN("ioct(EVIOCGBIT, EV_KEY) failed: %d %s\n", errno, strerror(errno));
221 close(fd);
222 continue;
223 }
224
225 /* A true joystick has at least axis X and Y, and at least 1
226 * button. copied from linux/drivers/input/joydev.c */
227 if (!test_bit(joydev.absbits, ABS_X) || !test_bit(joydev.absbits, ABS_Y) ||
228 !(test_bit(joydev.keybits, BTN_TRIGGER) ||
229 test_bit(joydev.keybits, BTN_A) ||
230 test_bit(joydev.keybits, BTN_1)))
231 {
232 close(fd);
233 continue;
234 }
235
236 if (!(joydev.device = HeapAlloc(GetProcessHeap(), 0, strlen(buf) + 1)))
237 {
238 close(fd);
239 continue;
240 }
241 strcpy(joydev.device, buf);
242
243 buf[MAX_PATH - 1] = 0;
244 if (ioctl(fd, EVIOCGNAME(MAX_PATH - 1), buf) != -1 &&
245 (joydev.name = HeapAlloc(GetProcessHeap(), 0, strlen(buf) + strlen(EVDEVDRIVER) + 1)))
246 {
247 strcpy(joydev.name, buf);
248 /* Append driver name */
249 strcat(joydev.name, EVDEVDRIVER);
250 }
251 else
252 joydev.name = joydev.device;
253
254 if (device_disabled_registry(joydev.name)) {
255 close(fd);
256 HeapFree(GetProcessHeap(), 0, joydev.name);
257 if (joydev.name != joydev.device)
258 HeapFree(GetProcessHeap(), 0, joydev.device);
259 continue;
260 }
261
262 joydev.guid = DInput_Wine_Joystick_Base_GUID;
263 joydev.guid.Data3 += have_joydevs;
264
265 TRACE("Found a joystick on %s: %s (%s)\n",
266 joydev.device, joydev.name,
267 debugstr_guid(&joydev.guid)
268 );
269
270 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
271 if (!no_ff_check &&
272 test_bit(joydev.evbits, EV_FF) &&
273 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(joydev.ffbits)), joydev.ffbits) != -1 &&
274 ioctl(fd, EVIOCGEFFECTS, &joydev.num_effects) != -1 &&
275 joydev.num_effects > 0)
276 {
277 TRACE(" ... with force feedback\n");
278 joydev.has_ff = 1;
279 }
280 #endif
281
282 for (j = 0; j < ABS_MAX;j ++)
283 {
284 if (!test_bit(joydev.absbits, j)) continue;
285 if (ioctl(fd, EVIOCGABS(j), &(joydev.axes[j])) != -1)
286 {
287 TRACE(" ... with axis %d: cur=%d, min=%d, max=%d, fuzz=%d, flat=%d\n",
288 j,
289 joydev.axes[j].value,
290 joydev.axes[j].minimum,
291 joydev.axes[j].maximum,
292 joydev.axes[j].fuzz,
293 joydev.axes[j].flat
294 );
295 }
296 }
297
298 if (ioctl(fd, EVIOCGID, &device_id) == -1)
299 WARN("ioct(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno, strerror(errno));
300 else
301 {
302 joydev.vendor_id = device_id.vendor;
303 joydev.product_id = device_id.product;
304 }
305
306 if (!have_joydevs)
307 new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
308 else
309 new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joydevs, (1 + have_joydevs) * sizeof(struct JoyDev));
310
311 if (!new_joydevs)
312 {
313 close(fd);
314 continue;
315 }
316 joydevs = new_joydevs;
317 joydevs[have_joydevs] = joydev;
318 have_joydevs++;
319
320 close(fd);
321 }
322 }
323
324 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
325 {
326 DWORD dwSize = lpddi->dwSize;
327
328 TRACE("%d %p\n", dwSize, lpddi);
329 memset(lpddi, 0, dwSize);
330
331 lpddi->dwSize = dwSize;
332 lpddi->guidInstance = joydevs[id].guid;
333 lpddi->guidProduct = DInput_Wine_Joystick_Base_GUID;
334 lpddi->guidFFDriver = GUID_NULL;
335
336 if (version >= 0x0800)
337 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
338 else
339 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
340
341 strcpy(lpddi->tszInstanceName, joydevs[id].name);
342 strcpy(lpddi->tszProductName, joydevs[id].name);
343 }
344
345 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
346 {
347 DWORD dwSize = lpddi->dwSize;
348
349 TRACE("%d %p\n", dwSize, lpddi);
350 memset(lpddi, 0, dwSize);
351
352 lpddi->dwSize = dwSize;
353 lpddi->guidInstance = joydevs[id].guid;
354 lpddi->guidProduct = DInput_Wine_Joystick_Base_GUID;
355 lpddi->guidFFDriver = GUID_NULL;
356
357 if (version >= 0x0800)
358 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
359 else
360 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
361
362 MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
363 MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszProductName, MAX_PATH);
364 }
365
366 static HRESULT joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
367 {
368 find_joydevs();
369
370 if (id >= have_joydevs) {
371 return E_FAIL;
372 }
373
374 if (!((dwDevType == 0) ||
375 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
376 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
377 return S_FALSE;
378
379 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
380 if (dwFlags & DIEDFL_FORCEFEEDBACK)
381 return S_FALSE;
382 #endif
383
384 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
385 fill_joystick_dideviceinstanceA(lpddi, version, id);
386 return S_OK;
387 }
388 return S_FALSE;
389 }
390
391 static HRESULT joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
392 {
393 find_joydevs();
394
395 if (id >= have_joydevs) {
396 return E_FAIL;
397 }
398
399 if (!((dwDevType == 0) ||
400 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
401 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
402 return S_FALSE;
403
404 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
405 if (dwFlags & DIEDFL_FORCEFEEDBACK)
406 return S_FALSE;
407 #endif
408
409 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
410 fill_joystick_dideviceinstanceW(lpddi, version, id);
411 return S_OK;
412 }
413 return S_FALSE;
414 }
415
416 static JoystickImpl *alloc_device(REFGUID rguid, IDirectInputImpl *dinput, unsigned short index)
417 {
418 JoystickImpl* newDevice;
419 LPDIDATAFORMAT df = NULL;
420 int i, idx = 0;
421 int default_axis_map[WINE_JOYSTICK_MAX_AXES + WINE_JOYSTICK_MAX_POVS*2];
422
423 newDevice = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(JoystickImpl));
424 if (!newDevice) return NULL;
425
426 newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
427 newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
428 newDevice->generic.base.ref = 1;
429 newDevice->generic.base.guid = *rguid;
430 newDevice->generic.base.dinput = dinput;
431 newDevice->generic.joy_polldev = joy_polldev;
432 newDevice->joyfd = -1;
433 newDevice->joydev = &joydevs[index];
434 newDevice->generic.name = newDevice->joydev->name;
435 list_init(&newDevice->ff_effects);
436 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
437 newDevice->ff_state = FF_STATUS_STOPPED;
438 #endif
439 /* There is no way in linux to query force feedback autocenter status.
440 Instead, track it with ff_autocenter, and assume it's initially
441 enabled. */
442 newDevice->ff_autocenter = 1;
443 newDevice->ff_gain = 0xFFFF;
444 InitializeCriticalSection(&newDevice->generic.base.crit);
445 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
446
447 /* Count number of available axes - supported Axis & POVs */
448 for (i = 0; i < ABS_MAX; i++)
449 {
450 if (i < WINE_JOYSTICK_MAX_AXES &&
451 test_bit(newDevice->joydev->absbits, i))
452 {
453 newDevice->generic.device_axis_count++;
454 newDevice->dev_axes_to_di[i] = idx;
455 newDevice->generic.props[idx].lDevMin = newDevice->joydev->axes[i].minimum;
456 newDevice->generic.props[idx].lDevMax = newDevice->joydev->axes[i].maximum;
457 default_axis_map[idx] = i;
458 idx++;
459 }
460 else
461 newDevice->dev_axes_to_di[i] = -1;
462 }
463
464 for (i = 0; i < WINE_JOYSTICK_MAX_POVS; i++)
465 {
466 if (test_bit(newDevice->joydev->absbits, ABS_HAT0X + i * 2) &&
467 test_bit(newDevice->joydev->absbits, ABS_HAT0Y + i * 2))
468 {
469 newDevice->generic.device_axis_count += 2;
470 newDevice->generic.props[idx ].lDevMin = newDevice->joydev->axes[ABS_HAT0X + i * 2].minimum;
471 newDevice->generic.props[idx ].lDevMax = newDevice->joydev->axes[ABS_HAT0X + i * 2].maximum;
472 newDevice->dev_axes_to_di[ABS_HAT0X + i * 2] = idx;
473 newDevice->generic.props[idx+1].lDevMin = newDevice->joydev->axes[ABS_HAT0Y + i * 2].minimum;
474 newDevice->generic.props[idx+1].lDevMax = newDevice->joydev->axes[ABS_HAT0Y + i * 2].maximum;
475 newDevice->dev_axes_to_di[ABS_HAT0Y + i * 2] = idx + 1;
476
477 default_axis_map[idx] = default_axis_map[idx + 1] = WINE_JOYSTICK_MAX_AXES + i;
478 idx += 2;
479 }
480 else
481 newDevice->dev_axes_to_di[ABS_HAT0X + i * 2] = newDevice->dev_axes_to_di[ABS_HAT0Y + i * 2] = -1;
482 }
483
484 /* do any user specified configuration */
485 if (setup_dinput_options(&newDevice->generic, default_axis_map) != DI_OK) goto failed;
486
487 /* Create copy of default data format */
488 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto failed;
489 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
490 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
491
492
493 /* Construct internal data format */
494
495 /* Supported Axis & POVs */
496 for (i = 0, idx = 0; i < newDevice->generic.device_axis_count; i++)
497 {
498 int wine_obj = newDevice->generic.axis_map[i];
499
500 if (wine_obj < 0) continue;
501
502 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
503 if (wine_obj < 8)
504 df->rgodf[idx].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
505 else
506 {
507 df->rgodf[idx].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
508 i++; /* POV takes 2 axes */
509 }
510
511 newDevice->generic.props[idx].lMin = 0;
512 newDevice->generic.props[idx].lMax = 0xffff;
513 newDevice->generic.props[idx].lSaturation = 0;
514 newDevice->generic.props[idx].lDeadZone = newDevice->generic.deadzone;
515
516 /* Linux supports force-feedback on X & Y axes only */
517 if (newDevice->joydev->has_ff && (i == 0 || i == 1))
518 df->rgodf[idx].dwFlags |= DIDOI_FFACTUATOR;
519
520 idx++;
521 }
522
523 /* Buttons can be anywhere, so check all */
524 for (i = 0; i < KEY_MAX && newDevice->generic.devcaps.dwButtons < WINE_JOYSTICK_MAX_BUTTONS; i++)
525 {
526 if (!test_bit(newDevice->joydev->keybits, i)) continue;
527
528 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[newDevice->generic.devcaps.dwButtons + 12], df->dwObjSize);
529 newDevice->buttons[i] = 0x80 | newDevice->generic.devcaps.dwButtons;
530 df->rgodf[idx ].pguid = &GUID_Button;
531 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->generic.devcaps.dwButtons++) | DIDFT_PSHBUTTON;
532 }
533 df->dwNumObjs = idx;
534 newDevice->generic.base.data_format.wine_df = df;
535
536 fake_current_js_state(newDevice);
537
538 /* Fill the caps */
539 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
540 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
541 if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
542 newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
543 else
544 newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
545
546 if (newDevice->joydev->has_ff)
547 newDevice->generic.devcaps.dwFlags |= DIDC_FORCEFEEDBACK;
548
549 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
550
551 EnterCriticalSection(&dinput->crit);
552 list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
553 LeaveCriticalSection(&dinput->crit);
554
555 return newDevice;
556
557 failed:
558 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
559 HeapFree(GetProcessHeap(), 0, df);
560 HeapFree(GetProcessHeap(), 0, newDevice->generic.axis_map);
561 HeapFree(GetProcessHeap(), 0, newDevice);
562 return NULL;
563 }
564
565 /******************************************************************************
566 * get_joystick_index : Get the joystick index from a given GUID
567 */
568 static unsigned short get_joystick_index(REFGUID guid)
569 {
570 GUID wine_joystick = DInput_Wine_Joystick_Base_GUID;
571 GUID dev_guid = *guid;
572
573 wine_joystick.Data3 = 0;
574 dev_guid.Data3 = 0;
575
576 /* for the standard joystick GUID use index 0 */
577 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
578
579 /* for the wine joystick GUIDs use the index stored in Data3 */
580 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3 - DInput_Wine_Joystick_Base_GUID.Data3;
581
582 return MAX_JOYDEV;
583 }
584
585 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
586 {
587 unsigned short index;
588
589 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
590 find_joydevs();
591 *pdev = NULL;
592
593 if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
594 have_joydevs && index < have_joydevs)
595 {
596 JoystickImpl *This;
597
598 if (riid == NULL)
599 ;/* nothing */
600 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
601 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
602 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
603 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
604 {
605 unicode = 0;
606 }
607 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
608 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
609 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
610 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
611 {
612 unicode = 1;
613 }
614 else
615 {
616 WARN("no interface\n");
617 return DIERR_NOINTERFACE;
618 }
619
620 This = alloc_device(rguid, dinput, index);
621 TRACE("Created a Joystick device (%p)\n", This);
622
623 if (!This) return DIERR_OUTOFMEMORY;
624
625 if (unicode)
626 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
627 else
628 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
629
630 return DI_OK;
631 }
632
633 return DIERR_DEVICENOTREG;
634 }
635
636
637 const struct dinput_device joystick_linuxinput_device = {
638 "Wine Linux-input joystick driver",
639 joydev_enum_deviceA,
640 joydev_enum_deviceW,
641 joydev_create_device
642 };
643
644 /******************************************************************************
645 * Acquire : gets exclusive control of the joystick
646 */
647 static HRESULT WINAPI JoystickWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
648 {
649 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
650 HRESULT res;
651
652 TRACE("(this=%p)\n",This);
653
654 if ((res = IDirectInputDevice2WImpl_Acquire(iface)) != DI_OK)
655 {
656 WARN("Failed to acquire: %x\n", res);
657 return res;
658 }
659
660 if ((This->joyfd = open(This->joydev->device, O_RDWR)) == -1)
661 {
662 if ((This->joyfd = open(This->joydev->device, O_RDONLY)) == -1)
663 {
664 /* Couldn't open the device at all */
665 ERR("Failed to open device %s: %d %s\n", This->joydev->device, errno, strerror(errno));
666 IDirectInputDevice2WImpl_Unacquire(iface);
667 return DIERR_NOTFOUND;
668 }
669 else
670 {
671 /* Couldn't open in r/w but opened in read-only. */
672 WARN("Could not open %s in read-write mode. Force feedback will be disabled.\n", This->joydev->device);
673 }
674 }
675 else
676 {
677 struct input_event event;
678
679 event.type = EV_FF;
680 event.code = FF_GAIN;
681 event.value = This->ff_gain;
682 if (write(This->joyfd, &event, sizeof(event)) == -1)
683 ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
684 if (!This->ff_autocenter)
685 {
686 /* Disable autocenter. */
687 event.code = FF_AUTOCENTER;
688 event.value = 0;
689 if (write(This->joyfd, &event, sizeof(event)) == -1)
690 ERR("Failed disabling autocenter: %d %s\n", errno, strerror(errno));
691 }
692 }
693
694 return DI_OK;
695 }
696
697 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
698 {
699 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
700 return JoystickWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
701 }
702
703 /******************************************************************************
704 * Unacquire : frees the joystick
705 */
706 static HRESULT WINAPI JoystickWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
707 {
708 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
709 HRESULT res;
710
711 TRACE("(this=%p)\n",This);
712 res = IDirectInputDevice2WImpl_Unacquire(iface);
713 if (res==DI_OK && This->joyfd!=-1) {
714 effect_list_item *itr;
715 struct input_event event;
716
717 /* For each known effect:
718 * - stop it
719 * - unload it
720 * But, unlike DISFFC_RESET, do not release the effect.
721 */
722 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry) {
723 IDirectInputEffect_Stop(itr->ref);
724 IDirectInputEffect_Unload(itr->ref);
725 }
726
727 /* Enable autocenter. */
728 event.type = EV_FF;
729 event.code = FF_AUTOCENTER;
730 /* TODO: Read autocenter strength before disabling it, and use it here
731 * instead of 0xFFFF (maximum strength).
732 */
733 event.value = 0xFFFF;
734 if (write(This->joyfd, &event, sizeof(event)) == -1)
735 ERR("Failed to set autocenter to %04x: %d %s\n", event.value, errno, strerror(errno));
736
737 close(This->joyfd);
738 This->joyfd = -1;
739 }
740 return res;
741 }
742
743 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
744 {
745 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
746 return JoystickWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
747 }
748
749 /*
750 * set the current state of the js device as it would be with the middle
751 * values on the axes
752 */
753 #define CENTER_AXIS(a) \
754 (ji->dev_axes_to_di[a] == -1 ? 0 : joystick_map_axis( &ji->generic.props[ji->dev_axes_to_di[a]], \
755 ji->joydev->axes[a].value ))
756 static void fake_current_js_state(JoystickImpl *ji)
757 {
758 int i;
759
760 /* center the axes */
761 ji->generic.js.lX = CENTER_AXIS(ABS_X);
762 ji->generic.js.lY = CENTER_AXIS(ABS_Y);
763 ji->generic.js.lZ = CENTER_AXIS(ABS_Z);
764 ji->generic.js.lRx = CENTER_AXIS(ABS_RX);
765 ji->generic.js.lRy = CENTER_AXIS(ABS_RY);
766 ji->generic.js.lRz = CENTER_AXIS(ABS_RZ);
767 ji->generic.js.rglSlider[0] = CENTER_AXIS(ABS_THROTTLE);
768 ji->generic.js.rglSlider[1] = CENTER_AXIS(ABS_RUDDER);
769
770 /* POV center is -1 */
771 for (i = 0; i < 4; i++)
772 ji->generic.js.rgdwPOV[i] = -1;
773 }
774 #undef CENTER_AXIS
775
776 /* convert wine format offset to user format object index */
777 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
778 {
779 struct pollfd plfd;
780 struct input_event ie;
781 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
782
783 if (This->joyfd==-1)
784 return;
785
786 while (1)
787 {
788 LONG value = 0;
789 int inst_id = -1;
790
791 plfd.fd = This->joyfd;
792 plfd.events = POLLIN;
793
794 if (poll(&plfd,1,0) != 1)
795 return;
796
797 /* we have one event, so we can read */
798 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
799 return;
800
801 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
802 switch (ie.type) {
803 case EV_KEY: /* button */
804 {
805 int btn = This->buttons[ie.code];
806
807 TRACE("(%p) %d -> %d\n", This, ie.code, btn);
808 if (btn & 0x80)
809 {
810 btn &= 0x7F;
811 inst_id = DIDFT_MAKEINSTANCE(btn) | DIDFT_PSHBUTTON;
812 This->generic.js.rgbButtons[btn] = value = ie.value ? 0x80 : 0x00;
813 }
814 break;
815 }
816 case EV_ABS:
817 {
818 int axis = This->dev_axes_to_di[ie.code];
819
820 /* User axis remapping */
821 if (axis < 0) break;
822 axis = This->generic.axis_map[axis];
823 if (axis < 0) break;
824
825 inst_id = axis < 8 ? DIDFT_MAKEINSTANCE(axis) | DIDFT_ABSAXIS :
826 DIDFT_MAKEINSTANCE(axis - 8) | DIDFT_POV;
827 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], ie.value);
828
829 switch (axis) {
830 case 0: This->generic.js.lX = value; break;
831 case 1: This->generic.js.lY = value; break;
832 case 2: This->generic.js.lZ = value; break;
833 case 3: This->generic.js.lRx = value; break;
834 case 4: This->generic.js.lRy = value; break;
835 case 5: This->generic.js.lRz = value; break;
836 case 6: This->generic.js.rglSlider[0] = value; break;
837 case 7: This->generic.js.rglSlider[1] = value; break;
838 case 8: case 9: case 10: case 11:
839 {
840 int idx = axis - 8;
841
842 if (ie.code % 2)
843 This->povs[idx].y = ie.value;
844 else
845 This->povs[idx].x = ie.value;
846
847 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
848 break;
849 }
850 default:
851 FIXME("unhandled joystick axis event (code %d, value %d)\n",ie.code,ie.value);
852 }
853 break;
854 }
855 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
856 case EV_FF_STATUS:
857 This->ff_state = ie.value;
858 break;
859 #endif
860 #ifdef EV_SYN
861 case EV_SYN:
862 /* there is nothing to do */
863 break;
864 #endif
865 #ifdef EV_MSC
866 case EV_MSC:
867 /* Ignore */
868 break;
869 #endif
870 default:
871 FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
872 break;
873 }
874 if (inst_id >= 0)
875 queue_event(iface, inst_id,
876 value, ie.time.tv_usec, This->generic.base.dinput->evsequence++);
877 }
878 }
879
880 /******************************************************************************
881 * SetProperty : change input device properties
882 */
883 static HRESULT WINAPI JoystickWImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
884 {
885 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
886
887 if (!ph) {
888 WARN("invalid argument\n");
889 return DIERR_INVALIDPARAM;
890 }
891
892 TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
893 TRACE("ph.dwSize = %d, ph.dwHeaderSize =%d, ph.dwObj = %d, ph.dwHow= %d\n",
894 ph->dwSize, ph->dwHeaderSize, ph->dwObj, ph->dwHow);
895
896 if (IS_DIPROP(rguid)) {
897 switch (LOWORD(rguid)) {
898 case (DWORD_PTR)DIPROP_CALIBRATIONMODE: {
899 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
900 FIXME("DIPROP_CALIBRATIONMODE(%d)\n", pd->dwData);
901 break;
902 }
903 case (DWORD_PTR)DIPROP_AUTOCENTER: {
904 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
905
906 TRACE("autocenter(%d)\n", pd->dwData);
907 This->ff_autocenter = pd->dwData == DIPROPAUTOCENTER_ON;
908
909 break;
910 }
911 case (DWORD_PTR)DIPROP_FFGAIN: {
912 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
913
914 TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
915 This->ff_gain = MulDiv(pd->dwData, 0xFFFF, 10000);
916 if (This->generic.base.acquired) {
917 /* Update immediately. */
918 struct input_event event;
919
920 event.type = EV_FF;
921 event.code = FF_GAIN;
922 event.value = This->ff_gain;
923 if (write(This->joyfd, &event, sizeof(event)) == -1)
924 ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
925 }
926 break;
927 }
928 default:
929 return JoystickWGenericImpl_SetProperty(iface, rguid, ph);
930 }
931 }
932 return DI_OK;
933 }
934
935 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
936 {
937 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
938 return JoystickWImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
939 }
940
941 /******************************************************************************
942 * GetProperty : get input device properties
943 */
944 static HRESULT WINAPI JoystickWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
945 {
946 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
947
948 TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
949 _dump_DIPROPHEADER(pdiph);
950
951 if (!IS_DIPROP(rguid)) return DI_OK;
952
953 switch (LOWORD(rguid)) {
954 case (DWORD_PTR) DIPROP_AUTOCENTER:
955 {
956 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
957
958 pd->dwData = This->ff_autocenter ? DIPROPAUTOCENTER_ON : DIPROPAUTOCENTER_OFF;
959 TRACE("autocenter(%d)\n", pd->dwData);
960 break;
961 }
962 case (DWORD_PTR) DIPROP_FFGAIN:
963 {
964 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
965
966 pd->dwData = MulDiv(This->ff_gain, 10000, 0xFFFF);
967 TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
968 break;
969 }
970
971 case (DWORD_PTR) DIPROP_VIDPID:
972 {
973 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
974
975 if (!This->joydev->product_id || !This->joydev->vendor_id)
976 return DIERR_UNSUPPORTED;
977 pd->dwData = MAKELONG(This->joydev->vendor_id, This->joydev->product_id);
978 TRACE("DIPROP_VIDPID(%08x)\n", pd->dwData);
979 break;
980 }
981
982 case (DWORD_PTR) DIPROP_JOYSTICKID:
983 {
984 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
985
986 pd->dwData = get_joystick_index(&This->generic.base.guid);
987 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
988 break;
989 }
990
991 default:
992 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
993 }
994
995 return DI_OK;
996 }
997
998 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
999 {
1000 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1001 return JoystickWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
1002 }
1003
1004 /******************************************************************************
1005 * CreateEffect - Create a new FF effect with the specified params
1006 */
1007 static HRESULT WINAPI JoystickWImpl_CreateEffect(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid,
1008 LPCDIEFFECT lpeff, LPDIRECTINPUTEFFECT *ppdef,
1009 LPUNKNOWN pUnkOuter)
1010 {
1011 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1012 effect_list_item* new_effect = NULL;
1013 HRESULT retval = DI_OK;
1014 #endif
1015
1016 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1017 TRACE("(this=%p,%p,%p,%p,%p)\n", This, rguid, lpeff, ppdef, pUnkOuter);
1018
1019 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
1020 TRACE("not available (compiled w/o ff support)\n");
1021 *ppdef = NULL;
1022 return DI_OK;
1023 #else
1024
1025 if (!(new_effect = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_effect))))
1026 return DIERR_OUTOFMEMORY;
1027
1028 retval = linuxinput_create_effect(&This->joyfd, rguid, &new_effect->entry, &new_effect->ref);
1029 if (retval != DI_OK)
1030 {
1031 HeapFree(GetProcessHeap(), 0, new_effect);
1032 return retval;
1033 }
1034
1035 if (lpeff != NULL)
1036 {
1037 retval = IDirectInputEffect_SetParameters(new_effect->ref, lpeff, 0);
1038
1039 if (retval != DI_OK && retval != DI_DOWNLOADSKIPPED)
1040 {
1041 HeapFree(GetProcessHeap(), 0, new_effect);
1042 return retval;
1043 }
1044 }
1045
1046 list_add_tail(&This->ff_effects, &new_effect->entry);
1047 *ppdef = new_effect->ref;
1048
1049 if (pUnkOuter != NULL)
1050 FIXME("Interface aggregation not implemented.\n");
1051
1052 return DI_OK;
1053
1054 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */
1055 }
1056
1057 static HRESULT WINAPI JoystickAImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid,
1058 LPCDIEFFECT lpeff, LPDIRECTINPUTEFFECT *ppdef,
1059 LPUNKNOWN pUnkOuter)
1060 {
1061 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1062 return JoystickWImpl_CreateEffect(IDirectInputDevice8W_from_impl(This), rguid, lpeff, ppdef, pUnkOuter);
1063 }
1064
1065 /*******************************************************************************
1066 * EnumEffects - Enumerate available FF effects
1067 */
1068 static HRESULT WINAPI JoystickAImpl_EnumEffects(LPDIRECTINPUTDEVICE8A iface,
1069 LPDIENUMEFFECTSCALLBACKA lpCallback,
1070 LPVOID pvRef,
1071 DWORD dwEffType)
1072 {
1073 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1074 DIEFFECTINFOA dei; /* feif */
1075 DWORD type = DIEFT_GETTYPE(dwEffType);
1076 JoystickImpl* This = impl_from_IDirectInputDevice8A(iface);
1077
1078 TRACE("(this=%p,%p,%d) type=%d\n", This, pvRef, dwEffType, type);
1079
1080 dei.dwSize = sizeof(DIEFFECTINFOA);
1081
1082 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1083 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1084 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1085 (*lpCallback)(&dei, pvRef);
1086 }
1087
1088 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1089 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1090 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1091 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1092 (*lpCallback)(&dei, pvRef);
1093 }
1094 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1095 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1096 (*lpCallback)(&dei, pvRef);
1097 }
1098 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1099 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1100 (*lpCallback)(&dei, pvRef);
1101 }
1102 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1103 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1104 (*lpCallback)(&dei, pvRef);
1105 }
1106 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1107 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1108 (*lpCallback)(&dei, pvRef);
1109 }
1110 }
1111
1112 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1113 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1114 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1115 (*lpCallback)(&dei, pvRef);
1116 }
1117
1118 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1119 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1120 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1121 (*lpCallback)(&dei, pvRef);
1122 }
1123 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1124 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1125 (*lpCallback)(&dei, pvRef);
1126 }
1127 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1128 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1129 (*lpCallback)(&dei, pvRef);
1130 }
1131 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1132 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1133 (*lpCallback)(&dei, pvRef);
1134 }
1135 }
1136
1137 #endif
1138
1139 return DI_OK;
1140 }
1141
1142 static HRESULT WINAPI JoystickWImpl_EnumEffects(LPDIRECTINPUTDEVICE8W iface,
1143 LPDIENUMEFFECTSCALLBACKW lpCallback,
1144 LPVOID pvRef,
1145 DWORD dwEffType)
1146 {
1147 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1148 /* seems silly to duplicate all this code but all the structures and functions
1149 * are actually different (A/W) */
1150 DIEFFECTINFOW dei; /* feif */
1151 DWORD type = DIEFT_GETTYPE(dwEffType);
1152 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1153 int xfd = This->joyfd;
1154
1155 TRACE("(this=%p,%p,%d) type=%d fd=%d\n", This, pvRef, dwEffType, type, xfd);
1156
1157 dei.dwSize = sizeof(DIEFFECTINFOW);
1158
1159 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1160 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1161 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1162 (*lpCallback)(&dei, pvRef);
1163 }
1164
1165 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1166 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1167 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1168 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1169 (*lpCallback)(&dei, pvRef);
1170 }
1171 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1172 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1173 (*lpCallback)(&dei, pvRef);
1174 }
1175 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1176 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1177 (*lpCallback)(&dei, pvRef);
1178 }
1179 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1180 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1181 (*lpCallback)(&dei, pvRef);
1182 }
1183 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1184 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1185 (*lpCallback)(&dei, pvRef);
1186 }
1187 }
1188
1189 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1190 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1191 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1192 (*lpCallback)(&dei, pvRef);
1193 }
1194
1195 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1196 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1197 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1198 (*lpCallback)(&dei, pvRef);
1199 }
1200 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1201 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1202 (*lpCallback)(&dei, pvRef);
1203 }
1204 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1205 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1206 (*lpCallback)(&dei, pvRef);
1207 }
1208 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1209 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1210 (*lpCallback)(&dei, pvRef);
1211 }
1212 }
1213
1214 /* return to unacquired state if that's where it was */
1215 if (xfd == -1)
1216 IDirectInputDevice8_Unacquire(iface);
1217 #endif
1218
1219 return DI_OK;
1220 }
1221
1222 /*******************************************************************************
1223 * GetEffectInfo - Get information about a particular effect
1224 */
1225 static HRESULT WINAPI JoystickAImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8A iface,
1226 LPDIEFFECTINFOA pdei,
1227 REFGUID guid)
1228 {
1229 JoystickImpl* This = impl_from_IDirectInputDevice8A(iface);
1230
1231 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1232
1233 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1234 return linuxinput_get_info_A(This->joyfd, guid, pdei);
1235 #else
1236 return DI_OK;
1237 #endif
1238 }
1239
1240 static HRESULT WINAPI JoystickWImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8W iface,
1241 LPDIEFFECTINFOW pdei,
1242 REFGUID guid)
1243 {
1244 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1245
1246 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1247
1248 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1249 return linuxinput_get_info_W(This->joyfd, guid, pdei);
1250 #else
1251 return DI_OK;
1252 #endif
1253 }
1254
1255 /*******************************************************************************
1256 * GetForceFeedbackState - Get information about the device's FF state
1257 */
1258 static HRESULT WINAPI JoystickWImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8W iface, LPDWORD pdwOut)
1259 {
1260 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1261
1262 TRACE("(this=%p,%p)\n", This, pdwOut);
1263
1264 (*pdwOut) = 0;
1265
1266 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1267 /* DIGFFS_STOPPED is the only mandatory flag to report */
1268 if (This->ff_state == FF_STATUS_STOPPED)
1269 (*pdwOut) |= DIGFFS_STOPPED;
1270 #endif
1271
1272 return DI_OK;
1273 }
1274
1275 static HRESULT WINAPI JoystickAImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8A iface, LPDWORD pdwOut)
1276 {
1277 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1278 return JoystickWImpl_GetForceFeedbackState(IDirectInputDevice8W_from_impl(This), pdwOut);
1279 }
1280
1281 /*******************************************************************************
1282 * SendForceFeedbackCommand - Send a command to the device's FF system
1283 */
1284 static HRESULT WINAPI JoystickWImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8W iface, DWORD dwFlags)
1285 {
1286 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1287 TRACE("(this=%p,%d)\n", This, dwFlags);
1288
1289 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1290 switch (dwFlags)
1291 {
1292 case DISFFC_STOPALL:
1293 {
1294 /* Stop all effects */
1295 effect_list_item *itr;
1296
1297 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry)
1298 IDirectInputEffect_Stop(itr->ref);
1299 break;
1300 }
1301
1302 case DISFFC_RESET:
1303 {
1304 effect_list_item *itr, *ptr;
1305
1306 /* Stop, unload, release and free all effects */
1307 /* This returns the device to its "bare" state */
1308 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1309 IDirectInputEffect_Release(itr->ref);
1310 break;
1311 }
1312 case DISFFC_PAUSE:
1313 case DISFFC_CONTINUE:
1314 FIXME("No support for Pause or Continue in linux\n");
1315 break;
1316
1317 case DISFFC_SETACTUATORSOFF:
1318 case DISFFC_SETACTUATORSON:
1319 FIXME("No direct actuator control in linux\n");
1320 break;
1321
1322 default:
1323 FIXME("Unknown Force Feedback Command!\n");
1324 return DIERR_INVALIDPARAM;
1325 }
1326 return DI_OK;
1327 #else
1328 return DIERR_UNSUPPORTED;
1329 #endif
1330 }
1331
1332 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8A iface, DWORD dwFlags)
1333 {
1334 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1335 return JoystickWImpl_SendForceFeedbackCommand(IDirectInputDevice8W_from_impl(This), dwFlags);
1336 }
1337
1338 /*******************************************************************************
1339 * EnumCreatedEffectObjects - Enumerate all the effects that have been
1340 * created for this device.
1341 */
1342 static HRESULT WINAPI JoystickWImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8W iface,
1343 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1344 LPVOID pvRef, DWORD dwFlags)
1345 {
1346 /* this function is safe to call on non-ff-enabled builds */
1347 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1348 effect_list_item *itr, *ptr;
1349
1350 TRACE("(this=%p,%p,%p,%d)\n", This, lpCallback, pvRef, dwFlags);
1351
1352 if (!lpCallback)
1353 return DIERR_INVALIDPARAM;
1354
1355 if (dwFlags != 0)
1356 FIXME("Flags specified, but no flags exist yet (DX9)!\n");
1357
1358 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1359 (*lpCallback)(itr->ref, pvRef);
1360
1361 return DI_OK;
1362 }
1363
1364 static HRESULT WINAPI JoystickAImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8A iface,
1365 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1366 LPVOID pvRef, DWORD dwFlags)
1367 {
1368 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1369 return JoystickWImpl_EnumCreatedEffectObjects(IDirectInputDevice8W_from_impl(This), lpCallback, pvRef, dwFlags);
1370 }
1371
1372 /******************************************************************************
1373 * GetDeviceInfo : get information about a device's identity
1374 */
1375 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface,
1376 LPDIDEVICEINSTANCEA pdidi)
1377 {
1378 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1379
1380 TRACE("(%p) %p\n", This, pdidi);
1381
1382 if (pdidi == NULL) return E_POINTER;
1383 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1384 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)))
1385 return DIERR_INVALIDPARAM;
1386
1387 fill_joystick_dideviceinstanceA(pdidi, This->generic.base.dinput->dwVersion,
1388 get_joystick_index(&This->generic.base.guid));
1389 return DI_OK;
1390 }
1391
1392 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface,
1393 LPDIDEVICEINSTANCEW pdidi)
1394 {
1395 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
1396
1397 TRACE("(%p) %p\n", This, pdidi);
1398
1399 if (pdidi == NULL) return E_POINTER;
1400 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1401 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)))
1402 return DIERR_INVALIDPARAM;
1403
1404 fill_joystick_dideviceinstanceW(pdidi, This->generic.base.dinput->dwVersion,
1405 get_joystick_index(&This->generic.base.guid));
1406 return DI_OK;
1407 }
1408
1409 static const IDirectInputDevice8AVtbl JoystickAvt =
1410 {
1411 IDirectInputDevice2AImpl_QueryInterface,
1412 IDirectInputDevice2AImpl_AddRef,
1413 IDirectInputDevice2AImpl_Release,
1414 JoystickAGenericImpl_GetCapabilities,
1415 IDirectInputDevice2AImpl_EnumObjects,
1416 JoystickAImpl_GetProperty,
1417 JoystickAImpl_SetProperty,
1418 JoystickAImpl_Acquire,
1419 JoystickAImpl_Unacquire,
1420 JoystickAGenericImpl_GetDeviceState,
1421 IDirectInputDevice2AImpl_GetDeviceData,
1422 IDirectInputDevice2AImpl_SetDataFormat,
1423 IDirectInputDevice2AImpl_SetEventNotification,
1424 IDirectInputDevice2AImpl_SetCooperativeLevel,
1425 JoystickAGenericImpl_GetObjectInfo,
1426 JoystickAImpl_GetDeviceInfo,
1427 IDirectInputDevice2AImpl_RunControlPanel,
1428 IDirectInputDevice2AImpl_Initialize,
1429 JoystickAImpl_CreateEffect,
1430 JoystickAImpl_EnumEffects,
1431 JoystickAImpl_GetEffectInfo,
1432 JoystickAImpl_GetForceFeedbackState,
1433 JoystickAImpl_SendForceFeedbackCommand,
1434 JoystickAImpl_EnumCreatedEffectObjects,
1435 IDirectInputDevice2AImpl_Escape,
1436 JoystickAGenericImpl_Poll,
1437 IDirectInputDevice2AImpl_SendDeviceData,
1438 IDirectInputDevice7AImpl_EnumEffectsInFile,
1439 IDirectInputDevice7AImpl_WriteEffectToFile,
1440 JoystickAGenericImpl_BuildActionMap,
1441 JoystickAGenericImpl_SetActionMap,
1442 IDirectInputDevice8AImpl_GetImageInfo
1443 };
1444
1445 static const IDirectInputDevice8WVtbl JoystickWvt =
1446 {
1447 IDirectInputDevice2WImpl_QueryInterface,
1448 IDirectInputDevice2WImpl_AddRef,
1449 IDirectInputDevice2WImpl_Release,
1450 JoystickWGenericImpl_GetCapabilities,
1451 IDirectInputDevice2WImpl_EnumObjects,
1452 JoystickWImpl_GetProperty,
1453 JoystickWImpl_SetProperty,
1454 JoystickWImpl_Acquire,
1455 JoystickWImpl_Unacquire,
1456 JoystickWGenericImpl_GetDeviceState,
1457 IDirectInputDevice2WImpl_GetDeviceData,
1458 IDirectInputDevice2WImpl_SetDataFormat,
1459 IDirectInputDevice2WImpl_SetEventNotification,
1460 IDirectInputDevice2WImpl_SetCooperativeLevel,
1461 JoystickWGenericImpl_GetObjectInfo,
1462 JoystickWImpl_GetDeviceInfo,
1463 IDirectInputDevice2WImpl_RunControlPanel,
1464 IDirectInputDevice2WImpl_Initialize,
1465 JoystickWImpl_CreateEffect,
1466 JoystickWImpl_EnumEffects,
1467 JoystickWImpl_GetEffectInfo,
1468 JoystickWImpl_GetForceFeedbackState,
1469 JoystickWImpl_SendForceFeedbackCommand,
1470 JoystickWImpl_EnumCreatedEffectObjects,
1471 IDirectInputDevice2WImpl_Escape,
1472 JoystickWGenericImpl_Poll,
1473 IDirectInputDevice2WImpl_SendDeviceData,
1474 IDirectInputDevice7WImpl_EnumEffectsInFile,
1475 IDirectInputDevice7WImpl_WriteEffectToFile,
1476 JoystickWGenericImpl_BuildActionMap,
1477 JoystickWGenericImpl_SetActionMap,
1478 IDirectInputDevice8WImpl_GetImageInfo
1479 };
1480
1481 #else /* HAS_PROPER_HEADER */
1482
1483 const struct dinput_device joystick_linuxinput_device = {
1484 "Wine Linux-input joystick driver",
1485 NULL,
1486 NULL,
1487 NULL
1488 };
1489
1490 #endif /* HAS_PROPER_HEADER */