441097d8e211b2b7fe462d4567d5edc506d82c61
[reactos.git] / reactos / dll / directx / 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 HAVE_CORRECT_LINUXINPUT_H
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 HAVE_CORRECT_LINUXINPUT_H
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 BOOL 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 FALSE;
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 FALSE;
378
379 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
380 if (dwFlags & DIEDFL_FORCEFEEDBACK)
381 return FALSE;
382 #endif
383
384 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
385 fill_joystick_dideviceinstanceA(lpddi, version, id);
386 return TRUE;
387 }
388 return FALSE;
389 }
390
391 static BOOL 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 FALSE;
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 FALSE;
403
404 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
405 if (dwFlags & DIEDFL_FORCEFEEDBACK)
406 return FALSE;
407 #endif
408
409 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
410 fill_joystick_dideviceinstanceW(lpddi, version, id);
411 return TRUE;
412 }
413 return 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 < WINE_JOYSTICK_MAX_AXES; i++)
449 {
450 if (test_bit(newDevice->joydev->absbits, i))
451 {
452 newDevice->generic.device_axis_count++;
453 newDevice->dev_axes_to_di[i] = idx;
454 newDevice->generic.props[idx].lDevMin = newDevice->joydev->axes[i].minimum;
455 newDevice->generic.props[idx].lDevMax = newDevice->joydev->axes[i].maximum;
456 default_axis_map[idx] = i;
457 idx++;
458 }
459 else
460 newDevice->dev_axes_to_di[i] = -1;
461 }
462
463 for (i = 0; i < WINE_JOYSTICK_MAX_POVS; i++)
464 {
465 if (test_bit(newDevice->joydev->absbits, ABS_HAT0X + i * 2) &&
466 test_bit(newDevice->joydev->absbits, ABS_HAT0Y + i * 2))
467 {
468 newDevice->generic.device_axis_count += 2;
469 newDevice->generic.props[idx ].lDevMin = newDevice->joydev->axes[ABS_HAT0X + i * 2].minimum;
470 newDevice->generic.props[idx ].lDevMax = newDevice->joydev->axes[ABS_HAT0X + i * 2].maximum;
471 newDevice->dev_axes_to_di[ABS_HAT0X + i * 2] = idx;
472 newDevice->generic.props[idx+1].lDevMin = newDevice->joydev->axes[ABS_HAT0Y + i * 2].minimum;
473 newDevice->generic.props[idx+1].lDevMax = newDevice->joydev->axes[ABS_HAT0Y + i * 2].maximum;
474 newDevice->dev_axes_to_di[ABS_HAT0Y + i * 2] = idx + 1;
475
476 default_axis_map[idx] = default_axis_map[idx + 1] = WINE_JOYSTICK_MAX_AXES + i;
477 idx += 2;
478 }
479 else
480 newDevice->dev_axes_to_di[ABS_HAT0X + i * 2] = newDevice->dev_axes_to_di[ABS_HAT0Y + i * 2] = -1;
481 }
482
483 /* do any user specified configuration */
484 if (setup_dinput_options(&newDevice->generic, default_axis_map) != DI_OK) goto failed;
485
486 /* Create copy of default data format */
487 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto failed;
488 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
489 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
490
491
492 /* Construct internal data format */
493
494 /* Supported Axis & POVs */
495 for (i = 0, idx = 0; i < newDevice->generic.device_axis_count; i++)
496 {
497 int wine_obj = newDevice->generic.axis_map[i];
498
499 if (wine_obj < 0) continue;
500
501 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
502 if (wine_obj < 8)
503 df->rgodf[idx].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
504 else
505 {
506 df->rgodf[idx].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
507 i++; /* POV takes 2 axes */
508 }
509
510 newDevice->generic.props[idx].lMin = 0;
511 newDevice->generic.props[idx].lMax = 0xffff;
512 newDevice->generic.props[idx].lSaturation = 0;
513 newDevice->generic.props[idx].lDeadZone = newDevice->generic.deadzone;
514
515 /* Linux supports force-feedback on X & Y axes only */
516 if (newDevice->joydev->has_ff && (i == 0 || i == 1))
517 df->rgodf[idx].dwFlags |= DIDOI_FFACTUATOR;
518
519 idx++;
520 }
521
522 /* Buttons can be anywhere, so check all */
523 for (i = 0; i < KEY_MAX && newDevice->generic.devcaps.dwButtons < WINE_JOYSTICK_MAX_BUTTONS; i++)
524 {
525 if (!test_bit(newDevice->joydev->keybits, i)) continue;
526
527 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[newDevice->generic.devcaps.dwButtons + 12], df->dwObjSize);
528 newDevice->buttons[i] = 0x80 | newDevice->generic.devcaps.dwButtons;
529 df->rgodf[idx ].pguid = &GUID_Button;
530 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->generic.devcaps.dwButtons++) | DIDFT_PSHBUTTON;
531 }
532 df->dwNumObjs = idx;
533 newDevice->generic.base.data_format.wine_df = df;
534
535 fake_current_js_state(newDevice);
536
537 /* Fill the caps */
538 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
539 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
540 if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
541 newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
542 else
543 newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
544
545 if (newDevice->joydev->has_ff)
546 newDevice->generic.devcaps.dwFlags |= DIDC_FORCEFEEDBACK;
547
548 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
549
550 EnterCriticalSection(&dinput->crit);
551 list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
552 LeaveCriticalSection(&dinput->crit);
553
554 return newDevice;
555
556 failed:
557 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
558 HeapFree(GetProcessHeap(), 0, df);
559 HeapFree(GetProcessHeap(), 0, newDevice->generic.axis_map);
560 HeapFree(GetProcessHeap(), 0, newDevice);
561 return NULL;
562 }
563
564 /******************************************************************************
565 * get_joystick_index : Get the joystick index from a given GUID
566 */
567 static unsigned short get_joystick_index(REFGUID guid)
568 {
569 GUID wine_joystick = DInput_Wine_Joystick_Base_GUID;
570 GUID dev_guid = *guid;
571
572 wine_joystick.Data3 = 0;
573 dev_guid.Data3 = 0;
574
575 /* for the standard joystick GUID use index 0 */
576 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
577
578 /* for the wine joystick GUIDs use the index stored in Data3 */
579 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3 - DInput_Wine_Joystick_Base_GUID.Data3;
580
581 return MAX_JOYDEV;
582 }
583
584 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
585 {
586 unsigned short index;
587
588 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
589 find_joydevs();
590 *pdev = NULL;
591
592 if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
593 have_joydevs && index < have_joydevs)
594 {
595 JoystickImpl *This;
596
597 if (riid == NULL)
598 ;/* nothing */
599 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
600 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
601 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
602 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
603 {
604 unicode = 0;
605 }
606 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
607 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
608 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
609 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
610 {
611 unicode = 1;
612 }
613 else
614 {
615 WARN("no interface\n");
616 return DIERR_NOINTERFACE;
617 }
618
619 This = alloc_device(rguid, dinput, index);
620 TRACE("Created a Joystick device (%p)\n", This);
621
622 if (!This) return DIERR_OUTOFMEMORY;
623
624 if (unicode)
625 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
626 else
627 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
628
629 return DI_OK;
630 }
631
632 return DIERR_DEVICENOTREG;
633 }
634
635
636 const struct dinput_device joystick_linuxinput_device = {
637 "Wine Linux-input joystick driver",
638 joydev_enum_deviceA,
639 joydev_enum_deviceW,
640 joydev_create_device
641 };
642
643 /******************************************************************************
644 * Acquire : gets exclusive control of the joystick
645 */
646 static HRESULT WINAPI JoystickWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
647 {
648 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
649 HRESULT res;
650
651 TRACE("(this=%p)\n",This);
652
653 if ((res = IDirectInputDevice2WImpl_Acquire(iface)) != DI_OK)
654 {
655 WARN("Failed to acquire: %x\n", res);
656 return res;
657 }
658
659 if ((This->joyfd = open(This->joydev->device, O_RDWR)) == -1)
660 {
661 if ((This->joyfd = open(This->joydev->device, O_RDONLY)) == -1)
662 {
663 /* Couldn't open the device at all */
664 ERR("Failed to open device %s: %d %s\n", This->joydev->device, errno, strerror(errno));
665 IDirectInputDevice2WImpl_Unacquire(iface);
666 return DIERR_NOTFOUND;
667 }
668 else
669 {
670 /* Couldn't open in r/w but opened in read-only. */
671 WARN("Could not open %s in read-write mode. Force feedback will be disabled.\n", This->joydev->device);
672 }
673 }
674 else
675 {
676 struct input_event event;
677
678 event.type = EV_FF;
679 event.code = FF_GAIN;
680 event.value = This->ff_gain;
681 if (write(This->joyfd, &event, sizeof(event)) == -1)
682 ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
683 if (!This->ff_autocenter)
684 {
685 /* Disable autocenter. */
686 event.code = FF_AUTOCENTER;
687 event.value = 0;
688 if (write(This->joyfd, &event, sizeof(event)) == -1)
689 ERR("Failed disabling autocenter: %d %s\n", errno, strerror(errno));
690 }
691 }
692
693 return DI_OK;
694 }
695
696 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
697 {
698 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
699 return JoystickWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
700 }
701
702 /******************************************************************************
703 * Unacquire : frees the joystick
704 */
705 static HRESULT WINAPI JoystickWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
706 {
707 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
708 HRESULT res;
709
710 TRACE("(this=%p)\n",This);
711 res = IDirectInputDevice2WImpl_Unacquire(iface);
712 if (res==DI_OK && This->joyfd!=-1) {
713 effect_list_item *itr;
714 struct input_event event;
715
716 /* For each known effect:
717 * - stop it
718 * - unload it
719 * But, unlike DISFFC_RESET, do not release the effect.
720 */
721 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry) {
722 IDirectInputEffect_Stop(itr->ref);
723 IDirectInputEffect_Unload(itr->ref);
724 }
725
726 /* Enable autocenter. */
727 event.type = EV_FF;
728 event.code = FF_AUTOCENTER;
729 /* TODO: Read autocenter strength before disabling it, and use it here
730 * instead of 0xFFFF (maximum strength).
731 */
732 event.value = 0xFFFF;
733 if (write(This->joyfd, &event, sizeof(event)) == -1)
734 ERR("Failed to set autocenter to %04x: %d %s\n", event.value, errno, strerror(errno));
735
736 close(This->joyfd);
737 This->joyfd = -1;
738 }
739 return res;
740 }
741
742 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
743 {
744 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
745 return JoystickWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
746 }
747
748 /*
749 * set the current state of the js device as it would be with the middle
750 * values on the axes
751 */
752 #define CENTER_AXIS(a) \
753 (ji->dev_axes_to_di[a] == -1 ? 0 : joystick_map_axis( &ji->generic.props[ji->dev_axes_to_di[a]], \
754 ji->joydev->axes[a].value ))
755 static void fake_current_js_state(JoystickImpl *ji)
756 {
757 int i;
758
759 /* center the axes */
760 ji->generic.js.lX = CENTER_AXIS(ABS_X);
761 ji->generic.js.lY = CENTER_AXIS(ABS_Y);
762 ji->generic.js.lZ = CENTER_AXIS(ABS_Z);
763 ji->generic.js.lRx = CENTER_AXIS(ABS_RX);
764 ji->generic.js.lRy = CENTER_AXIS(ABS_RY);
765 ji->generic.js.lRz = CENTER_AXIS(ABS_RZ);
766 ji->generic.js.rglSlider[0] = CENTER_AXIS(ABS_THROTTLE);
767 ji->generic.js.rglSlider[1] = CENTER_AXIS(ABS_RUDDER);
768
769 /* POV center is -1 */
770 for (i = 0; i < 4; i++)
771 ji->generic.js.rgdwPOV[i] = -1;
772 }
773 #undef CENTER_AXIS
774
775 /* convert wine format offset to user format object index */
776 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
777 {
778 struct pollfd plfd;
779 struct input_event ie;
780 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
781
782 if (This->joyfd==-1)
783 return;
784
785 while (1)
786 {
787 LONG value = 0;
788 int inst_id = -1;
789
790 plfd.fd = This->joyfd;
791 plfd.events = POLLIN;
792
793 if (poll(&plfd,1,0) != 1)
794 return;
795
796 /* we have one event, so we can read */
797 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
798 return;
799
800 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
801 switch (ie.type) {
802 case EV_KEY: /* button */
803 {
804 int btn = This->buttons[ie.code];
805
806 TRACE("(%p) %d -> %d\n", This, ie.code, btn);
807 if (btn & 0x80)
808 {
809 btn &= 0x7F;
810 inst_id = DIDFT_MAKEINSTANCE(btn) | DIDFT_PSHBUTTON;
811 This->generic.js.rgbButtons[btn] = value = ie.value ? 0x80 : 0x00;
812 }
813 break;
814 }
815 case EV_ABS:
816 {
817 int axis = This->dev_axes_to_di[ie.code];
818
819 /* User axis remapping */
820 if (axis < 0) break;
821 axis = This->generic.axis_map[axis];
822 if (axis < 0) break;
823
824 inst_id = axis < 8 ? DIDFT_MAKEINSTANCE(axis) | DIDFT_ABSAXIS :
825 DIDFT_MAKEINSTANCE(axis - 8) | DIDFT_POV;
826 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], ie.value);
827
828 switch (axis) {
829 case 0: This->generic.js.lX = value; break;
830 case 1: This->generic.js.lY = value; break;
831 case 2: This->generic.js.lZ = value; break;
832 case 3: This->generic.js.lRx = value; break;
833 case 4: This->generic.js.lRy = value; break;
834 case 5: This->generic.js.lRz = value; break;
835 case 6: This->generic.js.rglSlider[0] = value; break;
836 case 7: This->generic.js.rglSlider[1] = value; break;
837 case 8: case 9: case 10: case 11:
838 {
839 int idx = axis - 8;
840
841 if (ie.code % 2)
842 This->povs[idx].y = ie.value;
843 else
844 This->povs[idx].x = ie.value;
845
846 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
847 break;
848 }
849 default:
850 FIXME("unhandled joystick axis event (code %d, value %d)\n",ie.code,ie.value);
851 }
852 break;
853 }
854 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
855 case EV_FF_STATUS:
856 This->ff_state = ie.value;
857 break;
858 #endif
859 #ifdef EV_SYN
860 case EV_SYN:
861 /* there is nothing to do */
862 break;
863 #endif
864 #ifdef EV_MSC
865 case EV_MSC:
866 /* Ignore */
867 break;
868 #endif
869 default:
870 FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
871 break;
872 }
873 if (inst_id >= 0)
874 queue_event(iface, inst_id,
875 value, ie.time.tv_usec, This->generic.base.dinput->evsequence++);
876 }
877 }
878
879 /******************************************************************************
880 * SetProperty : change input device properties
881 */
882 static HRESULT WINAPI JoystickWImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
883 {
884 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
885
886 if (!ph) {
887 WARN("invalid argument\n");
888 return DIERR_INVALIDPARAM;
889 }
890
891 TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
892 TRACE("ph.dwSize = %d, ph.dwHeaderSize =%d, ph.dwObj = %d, ph.dwHow= %d\n",
893 ph->dwSize, ph->dwHeaderSize, ph->dwObj, ph->dwHow);
894
895 if (IS_DIPROP(rguid)) {
896 switch (LOWORD(rguid)) {
897 case (DWORD_PTR)DIPROP_CALIBRATIONMODE: {
898 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
899 FIXME("DIPROP_CALIBRATIONMODE(%d)\n", pd->dwData);
900 break;
901 }
902 case (DWORD_PTR)DIPROP_AUTOCENTER: {
903 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
904
905 TRACE("autocenter(%d)\n", pd->dwData);
906 This->ff_autocenter = pd->dwData == DIPROPAUTOCENTER_ON;
907
908 break;
909 }
910 case (DWORD_PTR)DIPROP_FFGAIN: {
911 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
912
913 TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
914 This->ff_gain = MulDiv(pd->dwData, 0xFFFF, 10000);
915 if (This->generic.base.acquired) {
916 /* Update immediately. */
917 struct input_event event;
918
919 event.type = EV_FF;
920 event.code = FF_GAIN;
921 event.value = This->ff_gain;
922 if (write(This->joyfd, &event, sizeof(event)) == -1)
923 ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
924 }
925 break;
926 }
927 default:
928 return JoystickWGenericImpl_SetProperty(iface, rguid, ph);
929 }
930 }
931 return DI_OK;
932 }
933
934 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
935 {
936 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
937 return JoystickWImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
938 }
939
940 /******************************************************************************
941 * GetProperty : get input device properties
942 */
943 static HRESULT WINAPI JoystickWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
944 {
945 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
946
947 TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
948 _dump_DIPROPHEADER(pdiph);
949
950 if (!IS_DIPROP(rguid)) return DI_OK;
951
952 switch (LOWORD(rguid)) {
953 case (DWORD_PTR) DIPROP_AUTOCENTER:
954 {
955 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
956
957 pd->dwData = This->ff_autocenter ? DIPROPAUTOCENTER_ON : DIPROPAUTOCENTER_OFF;
958 TRACE("autocenter(%d)\n", pd->dwData);
959 break;
960 }
961 case (DWORD_PTR) DIPROP_FFGAIN:
962 {
963 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
964
965 pd->dwData = MulDiv(This->ff_gain, 10000, 0xFFFF);
966 TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
967 break;
968 }
969
970 case (DWORD_PTR) DIPROP_VIDPID:
971 {
972 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
973
974 if (!This->joydev->product_id || !This->joydev->vendor_id)
975 return DIERR_UNSUPPORTED;
976 pd->dwData = MAKELONG(This->joydev->vendor_id, This->joydev->product_id);
977 TRACE("DIPROP_VIDPID(%08x)\n", pd->dwData);
978 break;
979 }
980
981 case (DWORD_PTR) DIPROP_JOYSTICKID:
982 {
983 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
984
985 pd->dwData = get_joystick_index(&This->generic.base.guid);
986 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
987 break;
988 }
989
990 default:
991 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
992 }
993
994 return DI_OK;
995 }
996
997 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
998 {
999 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1000 return JoystickWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
1001 }
1002
1003 /******************************************************************************
1004 * CreateEffect - Create a new FF effect with the specified params
1005 */
1006 static HRESULT WINAPI JoystickWImpl_CreateEffect(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid,
1007 LPCDIEFFECT lpeff, LPDIRECTINPUTEFFECT *ppdef,
1008 LPUNKNOWN pUnkOuter)
1009 {
1010 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1011 effect_list_item* new_effect = NULL;
1012 HRESULT retval = DI_OK;
1013 #endif
1014
1015 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1016 TRACE("(this=%p,%p,%p,%p,%p)\n", This, rguid, lpeff, ppdef, pUnkOuter);
1017
1018 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
1019 TRACE("not available (compiled w/o ff support)\n");
1020 *ppdef = NULL;
1021 return DI_OK;
1022 #else
1023
1024 if (!(new_effect = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_effect))))
1025 return DIERR_OUTOFMEMORY;
1026
1027 retval = linuxinput_create_effect(&This->joyfd, rguid, &new_effect->entry, &new_effect->ref);
1028 if (retval != DI_OK)
1029 {
1030 HeapFree(GetProcessHeap(), 0, new_effect);
1031 return retval;
1032 }
1033
1034 if (lpeff != NULL)
1035 {
1036 retval = IDirectInputEffect_SetParameters(new_effect->ref, lpeff, 0);
1037
1038 if (retval != DI_OK && retval != DI_DOWNLOADSKIPPED)
1039 {
1040 HeapFree(GetProcessHeap(), 0, new_effect);
1041 return retval;
1042 }
1043 }
1044
1045 list_add_tail(&This->ff_effects, &new_effect->entry);
1046 *ppdef = new_effect->ref;
1047
1048 if (pUnkOuter != NULL)
1049 FIXME("Interface aggregation not implemented.\n");
1050
1051 return DI_OK;
1052
1053 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */
1054 }
1055
1056 static HRESULT WINAPI JoystickAImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid,
1057 LPCDIEFFECT lpeff, LPDIRECTINPUTEFFECT *ppdef,
1058 LPUNKNOWN pUnkOuter)
1059 {
1060 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1061 return JoystickWImpl_CreateEffect(IDirectInputDevice8W_from_impl(This), rguid, lpeff, ppdef, pUnkOuter);
1062 }
1063
1064 /*******************************************************************************
1065 * EnumEffects - Enumerate available FF effects
1066 */
1067 static HRESULT WINAPI JoystickAImpl_EnumEffects(LPDIRECTINPUTDEVICE8A iface,
1068 LPDIENUMEFFECTSCALLBACKA lpCallback,
1069 LPVOID pvRef,
1070 DWORD dwEffType)
1071 {
1072 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1073 DIEFFECTINFOA dei; /* feif */
1074 DWORD type = DIEFT_GETTYPE(dwEffType);
1075 JoystickImpl* This = impl_from_IDirectInputDevice8A(iface);
1076
1077 TRACE("(this=%p,%p,%d) type=%d\n", This, pvRef, dwEffType, type);
1078
1079 dei.dwSize = sizeof(DIEFFECTINFOA);
1080
1081 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1082 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1083 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1084 (*lpCallback)(&dei, pvRef);
1085 }
1086
1087 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1088 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1089 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1090 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1091 (*lpCallback)(&dei, pvRef);
1092 }
1093 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1094 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1095 (*lpCallback)(&dei, pvRef);
1096 }
1097 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1098 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1099 (*lpCallback)(&dei, pvRef);
1100 }
1101 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1102 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1103 (*lpCallback)(&dei, pvRef);
1104 }
1105 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1106 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1107 (*lpCallback)(&dei, pvRef);
1108 }
1109 }
1110
1111 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1112 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1113 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1114 (*lpCallback)(&dei, pvRef);
1115 }
1116
1117 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1118 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1119 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1120 (*lpCallback)(&dei, pvRef);
1121 }
1122 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1123 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1124 (*lpCallback)(&dei, pvRef);
1125 }
1126 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1127 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1128 (*lpCallback)(&dei, pvRef);
1129 }
1130 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1131 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1132 (*lpCallback)(&dei, pvRef);
1133 }
1134 }
1135
1136 #endif
1137
1138 return DI_OK;
1139 }
1140
1141 static HRESULT WINAPI JoystickWImpl_EnumEffects(LPDIRECTINPUTDEVICE8W iface,
1142 LPDIENUMEFFECTSCALLBACKW lpCallback,
1143 LPVOID pvRef,
1144 DWORD dwEffType)
1145 {
1146 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1147 /* seems silly to duplicate all this code but all the structures and functions
1148 * are actually different (A/W) */
1149 DIEFFECTINFOW dei; /* feif */
1150 DWORD type = DIEFT_GETTYPE(dwEffType);
1151 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1152 int xfd = This->joyfd;
1153
1154 TRACE("(this=%p,%p,%d) type=%d fd=%d\n", This, pvRef, dwEffType, type, xfd);
1155
1156 dei.dwSize = sizeof(DIEFFECTINFOW);
1157
1158 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1159 && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1160 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1161 (*lpCallback)(&dei, pvRef);
1162 }
1163
1164 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1165 && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1166 if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1167 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1168 (*lpCallback)(&dei, pvRef);
1169 }
1170 if (test_bit(This->joydev->ffbits, FF_SINE)) {
1171 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1172 (*lpCallback)(&dei, pvRef);
1173 }
1174 if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1175 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1176 (*lpCallback)(&dei, pvRef);
1177 }
1178 if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1179 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1180 (*lpCallback)(&dei, pvRef);
1181 }
1182 if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1183 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1184 (*lpCallback)(&dei, pvRef);
1185 }
1186 }
1187
1188 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1189 && test_bit(This->joydev->ffbits, FF_RAMP)) {
1190 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1191 (*lpCallback)(&dei, pvRef);
1192 }
1193
1194 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1195 if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1196 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1197 (*lpCallback)(&dei, pvRef);
1198 }
1199 if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1200 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1201 (*lpCallback)(&dei, pvRef);
1202 }
1203 if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1204 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1205 (*lpCallback)(&dei, pvRef);
1206 }
1207 if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1208 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1209 (*lpCallback)(&dei, pvRef);
1210 }
1211 }
1212
1213 /* return to unacquired state if that's where it was */
1214 if (xfd == -1)
1215 IDirectInputDevice8_Unacquire(iface);
1216 #endif
1217
1218 return DI_OK;
1219 }
1220
1221 /*******************************************************************************
1222 * GetEffectInfo - Get information about a particular effect
1223 */
1224 static HRESULT WINAPI JoystickAImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8A iface,
1225 LPDIEFFECTINFOA pdei,
1226 REFGUID guid)
1227 {
1228 JoystickImpl* This = impl_from_IDirectInputDevice8A(iface);
1229
1230 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1231
1232 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1233 return linuxinput_get_info_A(This->joyfd, guid, pdei);
1234 #else
1235 return DI_OK;
1236 #endif
1237 }
1238
1239 static HRESULT WINAPI JoystickWImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8W iface,
1240 LPDIEFFECTINFOW pdei,
1241 REFGUID guid)
1242 {
1243 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1244
1245 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1246
1247 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1248 return linuxinput_get_info_W(This->joyfd, guid, pdei);
1249 #else
1250 return DI_OK;
1251 #endif
1252 }
1253
1254 /*******************************************************************************
1255 * GetForceFeedbackState - Get information about the device's FF state
1256 */
1257 static HRESULT WINAPI JoystickWImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8W iface, LPDWORD pdwOut)
1258 {
1259 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1260
1261 TRACE("(this=%p,%p)\n", This, pdwOut);
1262
1263 (*pdwOut) = 0;
1264
1265 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1266 /* DIGFFS_STOPPED is the only mandatory flag to report */
1267 if (This->ff_state == FF_STATUS_STOPPED)
1268 (*pdwOut) |= DIGFFS_STOPPED;
1269 #endif
1270
1271 return DI_OK;
1272 }
1273
1274 static HRESULT WINAPI JoystickAImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8A iface, LPDWORD pdwOut)
1275 {
1276 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1277 return JoystickWImpl_GetForceFeedbackState(IDirectInputDevice8W_from_impl(This), pdwOut);
1278 }
1279
1280 /*******************************************************************************
1281 * SendForceFeedbackCommand - Send a command to the device's FF system
1282 */
1283 static HRESULT WINAPI JoystickWImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8W iface, DWORD dwFlags)
1284 {
1285 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1286 TRACE("(this=%p,%d)\n", This, dwFlags);
1287
1288 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1289 switch (dwFlags)
1290 {
1291 case DISFFC_STOPALL:
1292 {
1293 /* Stop all effects */
1294 effect_list_item *itr;
1295
1296 LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry)
1297 IDirectInputEffect_Stop(itr->ref);
1298 break;
1299 }
1300
1301 case DISFFC_RESET:
1302 {
1303 effect_list_item *itr, *ptr;
1304
1305 /* Stop, unload, release and free all effects */
1306 /* This returns the device to its "bare" state */
1307 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1308 IDirectInputEffect_Release(itr->ref);
1309 break;
1310 }
1311 case DISFFC_PAUSE:
1312 case DISFFC_CONTINUE:
1313 FIXME("No support for Pause or Continue in linux\n");
1314 break;
1315
1316 case DISFFC_SETACTUATORSOFF:
1317 case DISFFC_SETACTUATORSON:
1318 FIXME("No direct actuator control in linux\n");
1319 break;
1320
1321 default:
1322 FIXME("Unknown Force Feedback Command!\n");
1323 return DIERR_INVALIDPARAM;
1324 }
1325 return DI_OK;
1326 #else
1327 return DIERR_UNSUPPORTED;
1328 #endif
1329 }
1330
1331 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8A iface, DWORD dwFlags)
1332 {
1333 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1334 return JoystickWImpl_SendForceFeedbackCommand(IDirectInputDevice8W_from_impl(This), dwFlags);
1335 }
1336
1337 /*******************************************************************************
1338 * EnumCreatedEffectObjects - Enumerate all the effects that have been
1339 * created for this device.
1340 */
1341 static HRESULT WINAPI JoystickWImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8W iface,
1342 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1343 LPVOID pvRef, DWORD dwFlags)
1344 {
1345 /* this function is safe to call on non-ff-enabled builds */
1346 JoystickImpl* This = impl_from_IDirectInputDevice8W(iface);
1347 effect_list_item *itr, *ptr;
1348
1349 TRACE("(this=%p,%p,%p,%d)\n", This, lpCallback, pvRef, dwFlags);
1350
1351 if (!lpCallback)
1352 return DIERR_INVALIDPARAM;
1353
1354 if (dwFlags != 0)
1355 FIXME("Flags specified, but no flags exist yet (DX9)!\n");
1356
1357 LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1358 (*lpCallback)(itr->ref, pvRef);
1359
1360 return DI_OK;
1361 }
1362
1363 static HRESULT WINAPI JoystickAImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8A iface,
1364 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1365 LPVOID pvRef, DWORD dwFlags)
1366 {
1367 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1368 return JoystickWImpl_EnumCreatedEffectObjects(IDirectInputDevice8W_from_impl(This), lpCallback, pvRef, dwFlags);
1369 }
1370
1371 /******************************************************************************
1372 * GetDeviceInfo : get information about a device's identity
1373 */
1374 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface,
1375 LPDIDEVICEINSTANCEA pdidi)
1376 {
1377 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1378
1379 TRACE("(%p) %p\n", This, pdidi);
1380
1381 if (pdidi == NULL) return E_POINTER;
1382 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1383 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)))
1384 return DIERR_INVALIDPARAM;
1385
1386 fill_joystick_dideviceinstanceA(pdidi, This->generic.base.dinput->dwVersion,
1387 get_joystick_index(&This->generic.base.guid));
1388 return DI_OK;
1389 }
1390
1391 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface,
1392 LPDIDEVICEINSTANCEW pdidi)
1393 {
1394 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
1395
1396 TRACE("(%p) %p\n", This, pdidi);
1397
1398 if (pdidi == NULL) return E_POINTER;
1399 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1400 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)))
1401 return DIERR_INVALIDPARAM;
1402
1403 fill_joystick_dideviceinstanceW(pdidi, This->generic.base.dinput->dwVersion,
1404 get_joystick_index(&This->generic.base.guid));
1405 return DI_OK;
1406 }
1407
1408 static const IDirectInputDevice8AVtbl JoystickAvt =
1409 {
1410 IDirectInputDevice2AImpl_QueryInterface,
1411 IDirectInputDevice2AImpl_AddRef,
1412 IDirectInputDevice2AImpl_Release,
1413 JoystickAGenericImpl_GetCapabilities,
1414 IDirectInputDevice2AImpl_EnumObjects,
1415 JoystickAImpl_GetProperty,
1416 JoystickAImpl_SetProperty,
1417 JoystickAImpl_Acquire,
1418 JoystickAImpl_Unacquire,
1419 JoystickAGenericImpl_GetDeviceState,
1420 IDirectInputDevice2AImpl_GetDeviceData,
1421 IDirectInputDevice2AImpl_SetDataFormat,
1422 IDirectInputDevice2AImpl_SetEventNotification,
1423 IDirectInputDevice2AImpl_SetCooperativeLevel,
1424 JoystickAGenericImpl_GetObjectInfo,
1425 JoystickAImpl_GetDeviceInfo,
1426 IDirectInputDevice2AImpl_RunControlPanel,
1427 IDirectInputDevice2AImpl_Initialize,
1428 JoystickAImpl_CreateEffect,
1429 JoystickAImpl_EnumEffects,
1430 JoystickAImpl_GetEffectInfo,
1431 JoystickAImpl_GetForceFeedbackState,
1432 JoystickAImpl_SendForceFeedbackCommand,
1433 JoystickAImpl_EnumCreatedEffectObjects,
1434 IDirectInputDevice2AImpl_Escape,
1435 JoystickAGenericImpl_Poll,
1436 IDirectInputDevice2AImpl_SendDeviceData,
1437 IDirectInputDevice7AImpl_EnumEffectsInFile,
1438 IDirectInputDevice7AImpl_WriteEffectToFile,
1439 JoystickAGenericImpl_BuildActionMap,
1440 JoystickAGenericImpl_SetActionMap,
1441 IDirectInputDevice8AImpl_GetImageInfo
1442 };
1443
1444 static const IDirectInputDevice8WVtbl JoystickWvt =
1445 {
1446 IDirectInputDevice2WImpl_QueryInterface,
1447 IDirectInputDevice2WImpl_AddRef,
1448 IDirectInputDevice2WImpl_Release,
1449 JoystickWGenericImpl_GetCapabilities,
1450 IDirectInputDevice2WImpl_EnumObjects,
1451 JoystickWImpl_GetProperty,
1452 JoystickWImpl_SetProperty,
1453 JoystickWImpl_Acquire,
1454 JoystickWImpl_Unacquire,
1455 JoystickWGenericImpl_GetDeviceState,
1456 IDirectInputDevice2WImpl_GetDeviceData,
1457 IDirectInputDevice2WImpl_SetDataFormat,
1458 IDirectInputDevice2WImpl_SetEventNotification,
1459 IDirectInputDevice2WImpl_SetCooperativeLevel,
1460 JoystickWGenericImpl_GetObjectInfo,
1461 JoystickWImpl_GetDeviceInfo,
1462 IDirectInputDevice2WImpl_RunControlPanel,
1463 IDirectInputDevice2WImpl_Initialize,
1464 JoystickWImpl_CreateEffect,
1465 JoystickWImpl_EnumEffects,
1466 JoystickWImpl_GetEffectInfo,
1467 JoystickWImpl_GetForceFeedbackState,
1468 JoystickWImpl_SendForceFeedbackCommand,
1469 JoystickWImpl_EnumCreatedEffectObjects,
1470 IDirectInputDevice2WImpl_Escape,
1471 JoystickWGenericImpl_Poll,
1472 IDirectInputDevice2WImpl_SendDeviceData,
1473 IDirectInputDevice7WImpl_EnumEffectsInFile,
1474 IDirectInputDevice7WImpl_WriteEffectToFile,
1475 JoystickWGenericImpl_BuildActionMap,
1476 JoystickWGenericImpl_SetActionMap,
1477 IDirectInputDevice8WImpl_GetImageInfo
1478 };
1479
1480 #else /* HAVE_CORRECT_LINUXINPUT_H */
1481
1482 const struct dinput_device joystick_linuxinput_device = {
1483 "Wine Linux-input joystick driver",
1484 NULL,
1485 NULL,
1486 NULL
1487 };
1488
1489 #endif /* HAVE_CORRECT_LINUXINPUT_H */