4660ca440d091345c302642a491159465f31deee
[reactos.git] / reactos / lib / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <sys/fcntl.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 # include <sys/ioctl.h>
40 #endif
41 #include <errno.h>
42 #ifdef HAVE_SYS_ERRNO_H
43 # include <sys/errno.h>
44 #endif
45 #ifdef HAVE_LINUX_INPUT_H
46 # include <linux/input.h>
47 # if defined(EVIOCGBIT) && defined(EV_ABS) && defined(BTN_PINKIE)
48 # define HAVE_CORRECT_LINUXINPUT_H
49 # endif
50 #endif
51
52 #include "wine/debug.h"
53 #include "wine/unicode.h"
54 #include "windef.h"
55 #include "winbase.h"
56 #include "winerror.h"
57 #include "dinput.h"
58
59 #include "dinput_private.h"
60 #include "device_private.h"
61
62 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
63
64 #ifdef HAVE_CORRECT_LINUXINPUT_H
65
66 #define EVDEVPREFIX "/dev/input/event"
67
68 /* Wine joystick driver object instances */
69 #define WINE_JOYSTICK_AXIS_BASE 0
70 #define WINE_JOYSTICK_BUTTON_BASE 8
71
72 typedef struct EffectListItem EffectListItem;
73 struct EffectListItem
74 {
75 LPDIRECTINPUTEFFECT ref;
76 struct EffectListItem* next;
77 };
78
79 /* implemented in effect_linuxinput.c */
80 HRESULT linuxinput_create_effect(int* fd, REFGUID rguid, LPDIRECTINPUTEFFECT* peff);
81 HRESULT linuxinput_get_info_A(int fd, REFGUID rguid, LPDIEFFECTINFOA info);
82 HRESULT linuxinput_get_info_W(int fd, REFGUID rguid, LPDIEFFECTINFOW info);
83
84 typedef struct JoystickImpl JoystickImpl;
85 static const IDirectInputDevice8AVtbl JoystickAvt;
86 static const IDirectInputDevice8WVtbl JoystickWvt;
87 struct JoystickImpl
88 {
89 const void *lpVtbl;
90 LONG ref;
91 GUID guid;
92
93
94 /* The 'parent' DInput */
95 IDirectInputImpl *dinput;
96
97 /* joystick private */
98 /* what range and deadzone the game wants */
99 LONG wantmin[ABS_MAX];
100 LONG wantmax[ABS_MAX];
101 LONG deadz[ABS_MAX];
102
103 /* autodetecting ranges per axe by following movement */
104 LONG havemax[ABS_MAX];
105 LONG havemin[ABS_MAX];
106
107 int joyfd;
108
109 LPDIDATAFORMAT df;
110 HANDLE hEvent;
111 LPDIDEVICEOBJECTDATA data_queue;
112 int queue_head, queue_tail, queue_len;
113 BOOL overflow;
114 DIJOYSTATE2 js;
115
116 /* Force feedback variables */
117 BOOL has_ff;
118 int num_effects;
119 EffectListItem* top_effect;
120 int ff_state;
121
122 /* data returned by the EVIOCGABS() ioctl */
123 int axes[ABS_MAX+1][5];
124
125 #define AXE_ABS 0
126 #define AXE_ABSMIN 1
127 #define AXE_ABSMAX 2
128 #define AXE_ABSFUZZ 3
129 #define AXE_ABSFLAT 4
130
131
132 /* data returned by EVIOCGBIT for caps, EV_ABS, EV_KEY, and EV_FF */
133 BYTE evbits[(EV_MAX+7)/8];
134 BYTE absbits[(ABS_MAX+7)/8];
135 BYTE keybits[(KEY_MAX+7)/8];
136 BYTE ffbits[(FF_MAX+7)/8];
137 };
138
139 /* This GUID is slightly different from the linux joystick one. Take note. */
140 static GUID DInput_Wine_Joystick_GUID = { /* 9e573eda-7734-11d2-8d4a-23903fb6bdf7 */
141 0x9e573eda,
142 0x7734,
143 0x11d2,
144 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
145 };
146
147 static void fake_current_js_state(JoystickImpl *ji);
148
149 #define test_bit(arr,bit) (((BYTE*)arr)[bit>>3]&(1<<(bit&7)))
150
151 static int joydev_have(BOOL require_ff)
152 {
153 int i, fd, flags, num_effects;
154 int havejoy = 0;
155
156 for (i=0;i<64;i++) {
157 char buf[200];
158 BYTE absbits[(ABS_MAX+7)/8],keybits[(KEY_MAX+7)/8];
159 BYTE evbits[(EV_MAX+7)/8],ffbits[(FF_MAX+7)/8];
160
161 sprintf(buf,EVDEVPREFIX"%d",i);
162
163 if (require_ff)
164 flags = O_RDWR;
165 else
166 flags = O_RDONLY;
167
168 if (-1!=(fd=open(buf,flags))) {
169 if (-1==ioctl(fd,EVIOCGBIT(EV_ABS,sizeof(absbits)),absbits)) {
170 perror("EVIOCGBIT EV_ABS");
171 close(fd);
172 continue;
173 }
174 if (-1==ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(keybits)),keybits)) {
175 perror("EVIOCGBIT EV_KEY");
176 close(fd);
177 continue;
178 }
179
180 /* test for force feedback if it's required */
181 if (require_ff) {
182 if ((-1==ioctl(fd,EVIOCGBIT(0,sizeof(evbits)),evbits))) {
183 perror("EVIOCGBIT 0");
184 close(fd);
185 continue;
186 }
187 if ( (!test_bit(evbits,EV_FF))
188 || (-1==ioctl(fd,EVIOCGBIT(EV_FF,sizeof(ffbits)),ffbits))
189 || (-1==ioctl(fd,EVIOCGEFFECTS,&num_effects))
190 || (num_effects <= 0)) {
191 close(fd);
192 continue;
193 }
194 }
195
196 /* A true joystick has at least axis X and Y, and at least 1
197 * button. copied from linux/drivers/input/joydev.c */
198 if (test_bit(absbits,ABS_X) && test_bit(absbits,ABS_Y) &&
199 ( test_bit(keybits,BTN_TRIGGER) ||
200 test_bit(keybits,BTN_A) ||
201 test_bit(keybits,BTN_1)
202 )
203 ) {
204 FIXME("found a joystick at %s!\n",buf);
205 havejoy = 1;
206 }
207 close(fd);
208 }
209 if (havejoy || (errno==ENODEV))
210 break;
211 }
212 return havejoy;
213 }
214
215 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
216 {
217 int havejoy = 0;
218
219 if (id != 0)
220 return FALSE;
221
222 if (!((dwDevType == 0) ||
223 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version < 0x0800)) ||
224 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
225 return FALSE;
226
227 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
228 if (dwFlags & DIEDFL_FORCEFEEDBACK)
229 return FALSE;
230 #endif
231
232 havejoy = joydev_have(dwFlags & DIEDFL_FORCEFEEDBACK);
233
234 if (!havejoy)
235 return FALSE;
236
237 TRACE("Enumerating the linuxinput Joystick device\n");
238
239 /* Return joystick */
240 lpddi->guidInstance = GUID_Joystick;
241 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
242
243 lpddi->guidFFDriver = GUID_NULL;
244 if (version >= 0x0800)
245 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
246 else
247 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
248
249 strcpy(lpddi->tszInstanceName, "Joystick");
250 /* ioctl JSIOCGNAME(len) */
251 strcpy(lpddi->tszProductName, "Wine Joystick");
252 return TRUE;
253 }
254
255 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
256 {
257 int havejoy = 0;
258
259 if (id != 0)
260 return FALSE;
261
262 if (!((dwDevType == 0) ||
263 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version < 0x0800)) ||
264 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
265 return FALSE;
266
267 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
268 if (dwFlags & DIEDFL_FORCEFEEDBACK)
269 return FALSE;
270 #endif
271
272 havejoy = joydev_have(dwFlags & DIEDFL_FORCEFEEDBACK);
273
274 if (!havejoy)
275 return FALSE;
276
277 TRACE("Enumerating the linuxinput Joystick device\n");
278
279 /* Return joystick */
280 lpddi->guidInstance = GUID_Joystick;
281 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
282
283 lpddi->guidFFDriver = GUID_NULL;
284 if (version >= 0x0800)
285 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
286 else
287 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
288
289 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, lpddi->tszInstanceName, MAX_PATH);
290 /* ioctl JSIOCGNAME(len) */
291 MultiByteToWideChar(CP_ACP, 0, "Wine Joystick", -1, lpddi->tszProductName, MAX_PATH);
292 return TRUE;
293 }
294
295 static JoystickImpl *alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput)
296 {
297 JoystickImpl* newDevice;
298 int i;
299
300 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
301 newDevice->lpVtbl = jvt;
302 newDevice->ref = 1;
303 newDevice->joyfd = -1;
304 newDevice->dinput = dinput;
305 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
306 newDevice->ff_state = FF_STATUS_STOPPED;
307 #endif
308 memcpy(&(newDevice->guid),rguid,sizeof(*rguid));
309 for (i=0;i<ABS_MAX;i++) {
310 newDevice->wantmin[i] = -32768;
311 newDevice->wantmax[i] = 32767;
312 /* TODO:
313 * direct input defines a default for the deadzone somewhere; but as long
314 * as in map_axis the code for the dead zone is commented out its no
315 * problem
316 */
317 newDevice->deadz[i] = 0;
318 }
319 fake_current_js_state(newDevice);
320 return newDevice;
321 }
322
323 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
324 {
325 int havejoy = 0;
326
327 havejoy = joydev_have(FALSE);
328
329 if (!havejoy)
330 return DIERR_DEVICENOTREG;
331
332 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
333 (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) {
334 if ((riid == NULL) ||
335 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
336 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
337 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
338 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
339 *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &JoystickAvt, dinput);
340 TRACE("Creating a Joystick device (%p)\n", *pdev);
341 return DI_OK;
342 } else
343 return DIERR_NOINTERFACE;
344 }
345
346 return DIERR_DEVICENOTREG;
347 }
348
349
350 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
351 {
352 int havejoy = 0;
353
354 havejoy = joydev_have(FALSE);
355
356 if (!havejoy)
357 return DIERR_DEVICENOTREG;
358
359 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
360 (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) {
361 if ((riid == NULL) ||
362 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
363 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
364 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
365 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
366 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &JoystickWvt, dinput);
367 TRACE("Creating a Joystick device (%p)\n", *pdev);
368 return DI_OK;
369 } else
370 return DIERR_NOINTERFACE;
371 }
372
373 return DIERR_DEVICENOTREG;
374 }
375
376 const struct dinput_device joystick_linuxinput_device = {
377 "Wine Linux-input joystick driver",
378 joydev_enum_deviceA,
379 joydev_enum_deviceW,
380 joydev_create_deviceA,
381 joydev_create_deviceW
382 };
383
384 /******************************************************************************
385 * Joystick
386 */
387 static ULONG WINAPI JoystickAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
388 {
389 JoystickImpl *This = (JoystickImpl *)iface;
390 ULONG ref;
391
392 ref = InterlockedDecrement(&(This->ref));
393 if (ref)
394 return ref;
395
396 /* Reset the FF state, free all effects, etc */
397 IDirectInputDevice8_SendForceFeedbackCommand(iface, DISFFC_RESET);
398
399 /* Free the data queue */
400 HeapFree(GetProcessHeap(),0,This->data_queue);
401
402 /* Free the DataFormat */
403 HeapFree(GetProcessHeap(), 0, This->df);
404
405 HeapFree(GetProcessHeap(),0,This);
406 return 0;
407 }
408
409 /******************************************************************************
410 * SetDataFormat : the application can choose the format of the data
411 * the device driver sends back with GetDeviceState.
412 */
413 static HRESULT WINAPI JoystickAImpl_SetDataFormat(
414 LPDIRECTINPUTDEVICE8A iface,LPCDIDATAFORMAT df
415 )
416 {
417 JoystickImpl *This = (JoystickImpl *)iface;
418
419 TRACE("(this=%p,%p)\n",This,df);
420
421 _dump_DIDATAFORMAT(df);
422
423 /* Store the new data format */
424 This->df = HeapAlloc(GetProcessHeap(),0,df->dwSize);
425 memcpy(This->df, df, df->dwSize);
426 This->df->rgodf = HeapAlloc(GetProcessHeap(),0,df->dwNumObjs*df->dwObjSize);
427 memcpy(This->df->rgodf,df->rgodf,df->dwNumObjs*df->dwObjSize);
428
429 return 0;
430 }
431
432 /******************************************************************************
433 * Acquire : gets exclusive control of the joystick
434 */
435 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
436 {
437 int i;
438 JoystickImpl *This = (JoystickImpl *)iface;
439 char buf[200];
440 BOOL readonly = TRUE;
441
442 TRACE("(this=%p)\n",This);
443 if (This->joyfd!=-1)
444 return 0;
445 for (i=0;i<64;i++) {
446 sprintf(buf,EVDEVPREFIX"%d",i);
447 if (-1==(This->joyfd=open(buf,O_RDWR))) {
448 if (-1==(This->joyfd=open(buf,O_RDONLY))) {
449 /* Couldn't open the device at all */
450 if (errno==ENODEV)
451 return DIERR_NOTFOUND;
452 perror(buf);
453 continue;
454 }
455 else {
456 /* Couldn't open in r/w but opened in read-only. */
457 WARN("Could not open %s in read-write mode. Force feedback will be disabled.\n",buf);
458 }
459 }
460 else {
461 /* Opened device in read-write */
462 readonly = FALSE;
463 }
464 if ((-1!=ioctl(This->joyfd,EVIOCGBIT(0,sizeof(This->evbits)),This->evbits)) &&
465 (-1!=ioctl(This->joyfd,EVIOCGBIT(EV_ABS,sizeof(This->absbits)),This->absbits)) &&
466 (-1!=ioctl(This->joyfd,EVIOCGBIT(EV_KEY,sizeof(This->keybits)),This->keybits)) &&
467 (test_bit(This->absbits,ABS_X) && test_bit(This->absbits,ABS_Y) &&
468 (test_bit(This->keybits,BTN_TRIGGER)||
469 test_bit(This->keybits,BTN_A) ||
470 test_bit(This->keybits,BTN_1)
471 )
472 )
473 )
474 break;
475 close(This->joyfd);
476 This->joyfd = -1;
477 }
478 if (This->joyfd==-1)
479 return DIERR_NOTFOUND;
480
481 This->has_ff = FALSE;
482 This->num_effects = 0;
483
484 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
485 if (!readonly && test_bit(This->evbits, EV_FF)) {
486 if (-1!=ioctl(This->joyfd,EVIOCGBIT(EV_FF,sizeof(This->ffbits)),This->ffbits)) {
487 if (-1!=ioctl(This->joyfd,EVIOCGEFFECTS,&This->num_effects)
488 && This->num_effects > 0) {
489 This->has_ff = TRUE;
490 TRACE("Joystick seems to be capable of force feedback.\n");
491 }
492 else {
493 TRACE("Joystick does not support any effects, disabling force feedback.\n");
494 }
495 }
496 else {
497 TRACE("Could not get EV_FF bits; disabling force feedback.\n");
498 }
499 }
500 else {
501 TRACE("Force feedback disabled (device is readonly or joystick incapable).\n");
502 }
503 #endif
504
505 for (i=0;i<ABS_MAX;i++) {
506 if (test_bit(This->absbits,i)) {
507 if (-1==ioctl(This->joyfd,EVIOCGABS(i),&(This->axes[i])))
508 continue;
509 FIXME("axe %d: cur=%d, min=%d, max=%d, fuzz=%d, flat=%d\n",
510 i,
511 This->axes[i][AXE_ABS],
512 This->axes[i][AXE_ABSMIN],
513 This->axes[i][AXE_ABSMAX],
514 This->axes[i][AXE_ABSFUZZ],
515 This->axes[i][AXE_ABSFLAT]
516 );
517 This->havemin[i] = This->axes[i][AXE_ABSMIN];
518 This->havemax[i] = This->axes[i][AXE_ABSMAX];
519 }
520 }
521 MESSAGE("\n");
522
523 fake_current_js_state(This);
524
525 return 0;
526 }
527
528 /******************************************************************************
529 * Unacquire : frees the joystick
530 */
531 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
532 {
533 JoystickImpl *This = (JoystickImpl *)iface;
534
535 TRACE("(this=%p)\n",This);
536 if (This->joyfd!=-1) {
537 close(This->joyfd);
538 This->joyfd = -1;
539 return DI_OK;
540 }
541 else
542 return DI_NOEFFECT;
543 }
544
545 /*
546 * This maps the read value (from the input event) to a value in the
547 * 'wanted' range. It also autodetects the possible range of the axe and
548 * adapts values accordingly.
549 */
550 static int
551 map_axis(JoystickImpl* This, int axis, int val) {
552 int xmin = This->axes[axis][AXE_ABSMIN];
553 int xmax = This->axes[axis][AXE_ABSMAX];
554 int hmax = This->havemax[axis];
555 int hmin = This->havemin[axis];
556 int wmin = This->wantmin[axis];
557 int wmax = This->wantmax[axis];
558 int ret;
559
560 if (val > hmax) This->havemax[axis] = hmax = val;
561 if (val < hmin) This->havemin[axis] = hmin = val;
562
563 if (xmin == xmax) return val;
564
565 /* map the value from the hmin-hmax range into the wmin-wmax range */
566 ret = ((val-hmin) * (wmax-wmin)) / (hmax-hmin) + wmin;
567
568 TRACE("xmin=%d xmax=%d hmin=%d hmax=%d wmin=%d wmax=%d val=%d ret=%d\n", xmin, xmax, hmin, hmax, wmin, wmax, val, ret);
569
570 #if 0
571 /* deadzone doesn't work comfortably enough right now. needs more testing*/
572 if ((ret > -deadz/2 ) && (ret < deadz/2)) {
573 FIXME("%d in deadzone, return mid.\n",val);
574 return (wmax-wmin)/2+wmin;
575 }
576 #endif
577 return ret;
578 }
579
580 /*
581 * set the current state of the js device as it would be with the middle
582 * values on the axes
583 */
584 static void fake_current_js_state(JoystickImpl *ji)
585 {
586 ji->js.lX = map_axis(ji, ABS_X, ji->axes[ABS_X ][AXE_ABS]);
587 ji->js.lY = map_axis(ji, ABS_Y, ji->axes[ABS_Y ][AXE_ABS]);
588 ji->js.lZ = map_axis(ji, ABS_Z, ji->axes[ABS_Z ][AXE_ABS]);
589 ji->js.lRx = map_axis(ji, ABS_RX, ji->axes[ABS_RX][AXE_ABS]);
590 ji->js.lRy = map_axis(ji, ABS_RY, ji->axes[ABS_RY][AXE_ABS]);
591 ji->js.lRz = map_axis(ji, ABS_RZ, ji->axes[ABS_RZ][AXE_ABS]);
592 ji->js.rglSlider[0] = map_axis(ji, ABS_THROTTLE, ji->axes[ABS_THROTTLE][AXE_ABS]);
593 ji->js.rglSlider[1] = map_axis(ji, ABS_RUDDER, ji->axes[ABS_RUDDER ][AXE_ABS]);
594 }
595
596 static void joy_polldev(JoystickImpl *This) {
597 struct timeval tv;
598 fd_set readfds;
599 struct input_event ie;
600
601 if (This->joyfd==-1)
602 return;
603
604 while (1) {
605 memset(&tv,0,sizeof(tv));
606 FD_ZERO(&readfds);
607 FD_SET(This->joyfd,&readfds);
608
609 if (1>select(This->joyfd+1,&readfds,NULL,NULL,&tv))
610 return;
611
612 /* we have one event, so we can read */
613 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
614 return;
615
616 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
617 switch (ie.type) {
618 case EV_KEY: /* button */
619 switch (ie.code) {
620 case BTN_TRIGGER: /* normal flight stick */
621 case BTN_A: /* gamepad */
622 case BTN_1: /* generic */
623 This->js.rgbButtons[0] = ie.value?0x80:0x00;
624 GEN_EVENT(DIJOFS_BUTTON(0),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
625 break;
626 case BTN_THUMB:
627 case BTN_B:
628 case BTN_2:
629 This->js.rgbButtons[1] = ie.value?0x80:0x00;
630 GEN_EVENT(DIJOFS_BUTTON(1),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
631 break;
632 case BTN_THUMB2:
633 case BTN_C:
634 case BTN_3:
635 This->js.rgbButtons[2] = ie.value?0x80:0x00;
636 GEN_EVENT(DIJOFS_BUTTON(2),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
637 break;
638 case BTN_TOP:
639 case BTN_X:
640 case BTN_4:
641 This->js.rgbButtons[3] = ie.value?0x80:0x00;
642 GEN_EVENT(DIJOFS_BUTTON(3),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
643 break;
644 case BTN_TOP2:
645 case BTN_Y:
646 case BTN_5:
647 This->js.rgbButtons[4] = ie.value?0x80:0x00;
648 GEN_EVENT(DIJOFS_BUTTON(4),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
649 break;
650 case BTN_PINKIE:
651 case BTN_Z:
652 case BTN_6:
653 This->js.rgbButtons[5] = ie.value?0x80:0x00;
654 GEN_EVENT(DIJOFS_BUTTON(5),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
655 break;
656 case BTN_BASE:
657 case BTN_TL:
658 case BTN_7:
659 This->js.rgbButtons[6] = ie.value?0x80:0x00;
660 GEN_EVENT(DIJOFS_BUTTON(6),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
661 break;
662 case BTN_BASE2:
663 case BTN_TR:
664 case BTN_8:
665 This->js.rgbButtons[7] = ie.value?0x80:0x00;
666 GEN_EVENT(DIJOFS_BUTTON(7),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
667 break;
668 case BTN_BASE3:
669 case BTN_TL2:
670 case BTN_9:
671 This->js.rgbButtons[8] = ie.value?0x80:0x00;
672 GEN_EVENT(DIJOFS_BUTTON(8),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
673 break;
674 case BTN_BASE4:
675 case BTN_TR2:
676 This->js.rgbButtons[9] = ie.value?0x80:0x00;
677 GEN_EVENT(DIJOFS_BUTTON(9),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
678 break;
679 case BTN_BASE5:
680 case BTN_SELECT:
681 This->js.rgbButtons[10] = ie.value?0x80:0x00;
682 GEN_EVENT(DIJOFS_BUTTON(10),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
683 break;
684 default:
685 FIXME("unhandled joystick button %x, value %d\n",ie.code,ie.value);
686 break;
687 }
688 break;
689 case EV_ABS:
690 switch (ie.code) {
691 case ABS_X:
692 This->js.lX = map_axis(This,ABS_X,ie.value);
693 GEN_EVENT(DIJOFS_X,This->js.lX,ie.time.tv_usec,(This->dinput->evsequence)++);
694 break;
695 case ABS_Y:
696 This->js.lY = map_axis(This,ABS_Y,ie.value);
697 GEN_EVENT(DIJOFS_Y,This->js.lY,ie.time.tv_usec,(This->dinput->evsequence)++);
698 break;
699 case ABS_Z:
700 This->js.lZ = map_axis(This,ABS_Z,ie.value);
701 GEN_EVENT(DIJOFS_Z,This->js.lZ,ie.time.tv_usec,(This->dinput->evsequence)++);
702 break;
703 case ABS_RX:
704 This->js.lRx = map_axis(This,ABS_RX,ie.value);
705 GEN_EVENT(DIJOFS_RX,This->js.lRx,ie.time.tv_usec,(This->dinput->evsequence)++);
706 break;
707 case ABS_RY:
708 This->js.lRy = map_axis(This,ABS_RY,ie.value);
709 GEN_EVENT(DIJOFS_RY,This->js.lRy,ie.time.tv_usec,(This->dinput->evsequence)++);
710 break;
711 case ABS_RZ:
712 This->js.lRz = map_axis(This,ABS_RZ,ie.value);
713 GEN_EVENT(DIJOFS_RZ,This->js.lRz,ie.time.tv_usec,(This->dinput->evsequence)++);
714 break;
715 case ABS_THROTTLE:
716 This->js.rglSlider[0] = map_axis(This,ABS_THROTTLE,ie.value);
717 GEN_EVENT(DIJOFS_SLIDER(0),This->js.rglSlider[0],ie.time.tv_usec,(This->dinput->evsequence)++);
718 break;
719 case ABS_RUDDER:
720 This->js.rglSlider[1] = map_axis(This,ABS_RUDDER,ie.value);
721 GEN_EVENT(DIJOFS_SLIDER(1),This->js.rglSlider[1],ie.time.tv_usec,(This->dinput->evsequence)++);
722 break;
723 default:
724 FIXME("unhandled joystick axe event (code %d, value %d)\n",ie.code,ie.value);
725 break;
726 }
727 break;
728 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
729 case EV_FF_STATUS:
730 This->ff_state = ie.value;
731 break;
732 #endif
733 default:
734 FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
735 break;
736 }
737 }
738 }
739
740 /******************************************************************************
741 * GetDeviceState : returns the "state" of the joystick.
742 *
743 */
744 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
745 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
746 ) {
747 JoystickImpl *This = (JoystickImpl *)iface;
748
749 joy_polldev(This);
750
751 TRACE("(this=%p,0x%08lx,%p)\n",This,len,ptr);
752 if ((len != sizeof(DIJOYSTATE)) && (len != sizeof(DIJOYSTATE2))) {
753 FIXME("len %ld is not sizeof(DIJOYSTATE) or DIJOYSTATE2, unsupported format.\n",len);
754 return E_FAIL;
755 }
756 memcpy(ptr,&(This->js),len);
757 This->queue_head = 0;
758 This->queue_tail = 0;
759 return 0;
760 }
761
762 /******************************************************************************
763 * GetDeviceData : gets buffered input data.
764 */
765 static HRESULT WINAPI JoystickAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
766 DWORD dodsize,
767 LPDIDEVICEOBJECTDATA dod,
768 LPDWORD entries,
769 DWORD flags
770 ) {
771 JoystickImpl *This = (JoystickImpl *)iface;
772
773 FIXME("(%p)->(dods=%ld,entries=%ld,fl=0x%08lx),STUB!\n",This,dodsize,*entries,flags);
774
775 joy_polldev(This);
776 if (flags & DIGDD_PEEK)
777 FIXME("DIGDD_PEEK\n");
778
779 if (dod == NULL) {
780 } else {
781 }
782 return 0;
783 }
784
785 /******************************************************************************
786 * SetProperty : change input device properties
787 */
788 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface,
789 REFGUID rguid,
790 LPCDIPROPHEADER ph)
791 {
792 JoystickImpl *This = (JoystickImpl *)iface;
793
794 FIXME("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
795 FIXME("ph.dwSize = %ld, ph.dwHeaderSize =%ld, ph.dwObj = %ld, ph.dwHow= %ld\n",ph->dwSize, ph->dwHeaderSize,ph->dwObj,ph->dwHow);
796
797 if (!HIWORD(rguid)) {
798 switch (LOWORD(rguid)) {
799 case (DWORD) DIPROP_BUFFERSIZE: {
800 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
801
802 FIXME("buffersize = %ld\n",pd->dwData);
803 break;
804 }
805 case (DWORD)DIPROP_RANGE: {
806 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
807
808 FIXME("proprange(%ld,%ld)\n",pr->lMin,pr->lMax);
809 switch (ph->dwObj) {
810 case 0: /* X */
811 case 4: /* Y */
812 case 8: /* Z */
813 case 12: /* Rx */
814 case 16: /* Ry */
815 case 20: /* Rz */
816 case 24: /* Slider 0 -> Throttle */
817 case 28: /* Slider 1 -> Rudder */
818 This->wantmin[ph->dwObj/4] = pr->lMin;
819 This->wantmax[ph->dwObj/4] = pr->lMax;
820 break;
821 default:
822 FIXME("setting proprange %ld - %ld for dwObj %ld\n",pr->lMin,pr->lMax,ph->dwObj);
823 }
824 break;
825 }
826 case (DWORD)DIPROP_DEADZONE: {
827 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
828
829 FIXME("setting deadzone(%ld)\n",pd->dwData);
830 This->deadz[ph->dwObj/4] = pd->dwData;
831 break;
832 }
833 default:
834 FIXME("Unknown type %p (%s)\n",rguid,debugstr_guid(rguid));
835 break;
836 }
837 }
838 fake_current_js_state(This);
839 return 0;
840 }
841
842 /******************************************************************************
843 * SetEventNotification : specifies event to be sent on state change
844 */
845 static HRESULT WINAPI JoystickAImpl_SetEventNotification(
846 LPDIRECTINPUTDEVICE8A iface, HANDLE hnd
847 ) {
848 JoystickImpl *This = (JoystickImpl *)iface;
849
850 TRACE("(this=%p,%p)\n",This,hnd);
851 This->hEvent = hnd;
852 return DI_OK;
853 }
854
855 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
856 LPDIRECTINPUTDEVICE8A iface,
857 LPDIDEVCAPS lpDIDevCaps)
858 {
859 JoystickImpl *This = (JoystickImpl *)iface;
860 int xfd = This->joyfd;
861 int i,axes,buttons;
862 int wasacquired = 1;
863
864 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
865 if (xfd==-1) {
866 /* yes, games assume we return something, even if unacquired */
867 JoystickAImpl_Acquire(iface);
868 xfd = This->joyfd;
869 wasacquired = 0;
870 }
871 lpDIDevCaps->dwFlags = DIDC_ATTACHED;
872 if (This->dinput->dwVersion >= 0x0800)
873 lpDIDevCaps->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
874 else
875 lpDIDevCaps->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
876
877 axes=0;
878 for (i=0;i<ABS_MAX;i++) if (test_bit(This->absbits,i)) axes++;
879 buttons=0;
880 for (i=0;i<KEY_MAX;i++) if (test_bit(This->keybits,i)) buttons++;
881
882 if (This->has_ff)
883 lpDIDevCaps->dwFlags |= DIDC_FORCEFEEDBACK;
884
885 lpDIDevCaps->dwAxes = axes;
886 lpDIDevCaps->dwButtons = buttons;
887
888 if (!wasacquired)
889 JoystickAImpl_Unacquire(iface);
890
891 return DI_OK;
892 }
893
894 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface) {
895 JoystickImpl *This = (JoystickImpl *)iface;
896 TRACE("(),stub!\n");
897
898 joy_polldev(This);
899 return DI_OK;
900 }
901
902 /******************************************************************************
903 * EnumObjects : enumerate the different buttons and axis...
904 */
905 static HRESULT WINAPI JoystickAImpl_EnumObjects(
906 LPDIRECTINPUTDEVICE8A iface,
907 LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
908 LPVOID lpvRef,
909 DWORD dwFlags)
910 {
911 JoystickImpl *This = (JoystickImpl *)iface;
912 DIDEVICEOBJECTINSTANCEA ddoi;
913 int xfd = This->joyfd;
914
915 TRACE("(this=%p,%p,%p,%08lx)\n", This, lpCallback, lpvRef, dwFlags);
916 if (TRACE_ON(dinput)) {
917 TRACE(" - flags = ");
918 _dump_EnumObjects_flags(dwFlags);
919 TRACE("\n");
920 }
921
922 /* We need to work even if we're not yet acquired */
923 if (xfd == -1)
924 IDirectInputDevice8_Acquire(iface);
925
926 /* Only the fields till dwFFMaxForce are relevant */
927 ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
928
929 /* For the joystick, do as is done in the GetCapabilities function */
930 /* FIXME: needs more items */
931 if ((dwFlags == DIDFT_ALL) ||
932 (dwFlags & DIDFT_AXIS)) {
933 BYTE i;
934
935 for (i = 0; i < ABS_MAX; i++) {
936 if (!test_bit(This->absbits,i)) continue;
937
938 switch (i) {
939 case ABS_X:
940 ddoi.guidType = GUID_XAxis;
941 ddoi.dwOfs = DIJOFS_X;
942 break;
943 case ABS_Y:
944 ddoi.guidType = GUID_YAxis;
945 ddoi.dwOfs = DIJOFS_Y;
946 break;
947 case ABS_Z:
948 ddoi.guidType = GUID_ZAxis;
949 ddoi.dwOfs = DIJOFS_Z;
950 break;
951 case ABS_RX:
952 ddoi.guidType = GUID_RxAxis;
953 ddoi.dwOfs = DIJOFS_RX;
954 break;
955 case ABS_RY:
956 ddoi.guidType = GUID_RyAxis;
957 ddoi.dwOfs = DIJOFS_RY;
958 break;
959 case ABS_RZ:
960 ddoi.guidType = GUID_RzAxis;
961 ddoi.dwOfs = DIJOFS_RZ;
962 break;
963 case ABS_THROTTLE:
964 ddoi.guidType = GUID_Slider;
965 ddoi.dwOfs = DIJOFS_SLIDER(0);
966 break;
967 case ABS_RUDDER:
968 ddoi.guidType = GUID_Slider;
969 ddoi.dwOfs = DIJOFS_SLIDER(1);
970 break;
971 default:
972 FIXME("unhandled abs axis %d, ignoring!\n",i);
973 }
974 ddoi.dwType = DIDFT_MAKEINSTANCE((1<<i) << WINE_JOYSTICK_AXIS_BASE) | DIDFT_ABSAXIS;
975 /* Linux event force feedback supports only (and always) x and y axes */
976 if (i == ABS_X || i == ABS_Y) {
977 if (This->has_ff)
978 ddoi.dwFlags |= DIDOI_FFACTUATOR;
979 }
980 sprintf(ddoi.tszName, "%d-Axis", i);
981 _dump_OBJECTINSTANCEA(&ddoi);
982 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) {
983 /* return to unaquired state if that's where we were */
984 if (xfd == -1)
985 IDirectInputDevice8_Unacquire(iface);
986 return DI_OK;
987 }
988 }
989 }
990
991 if ((dwFlags == DIDFT_ALL) ||
992 (dwFlags & DIDFT_BUTTON)) {
993 int i;
994
995 /*The DInput SDK says that GUID_Button is only for mouse buttons but well*/
996
997 ddoi.guidType = GUID_Button;
998
999 for (i = 0; i < KEY_MAX; i++) {
1000 if (!test_bit(This->keybits,i)) continue;
1001
1002 switch (i) {
1003 case BTN_TRIGGER:
1004 case BTN_A:
1005 case BTN_1:
1006 ddoi.dwOfs = DIJOFS_BUTTON(0);
1007 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 0) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
1008 break;
1009 case BTN_THUMB:
1010 case BTN_B:
1011 case BTN_2:
1012 ddoi.dwOfs = DIJOFS_BUTTON(1);
1013 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 1) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
1014 break;
1015 case BTN_THUMB2:
1016 case BTN_C:
1017 case BTN_3:
1018 ddoi.dwOfs = DIJOFS_BUTTON(2);
1019 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 2) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
1020 break;
1021 case BTN_TOP:
1022 case BTN_X:
1023 case BTN_4:
1024 ddoi.dwOfs = DIJOFS_BUTTON(3);
1025 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 3) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
1026 break;
1027 case BTN_TOP2:
1028 case BTN_Y:
1029 case BTN_5:
1030 ddoi.dwOfs = DIJOFS_BUTTON(4);
1031 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 4) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
1032 break;
1033 case BTN_PINKIE:
1034 case BTN_Z:
1035 case BTN_6:
1036 ddoi.dwOfs = DIJOFS_BUTTON(5);
1037 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 5) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
1038 break;
1039 case BTN_BASE:
1040 case BTN_TL:
1041 case BTN_7:
1042 ddoi.dwOfs = DIJOFS_BUTTON(6);
1043 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 6) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
1044 break;
1045 case BTN_BASE2:
1046 case BTN_TR:
1047 case BTN_8:
1048 ddoi.dwOfs = DIJOFS_BUTTON(7);
1049 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 7) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
1050 break;
1051 case BTN_BASE3:
1052 case BTN_TL2:
1053 case BTN_9:
1054 ddoi.dwOfs = DIJOFS_BUTTON(8);
1055 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 8) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
1056 break;
1057 case BTN_BASE4:
1058 case BTN_TR2:
1059 ddoi.dwOfs = DIJOFS_BUTTON(9);
1060 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 9) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
1061 break;
1062 case BTN_BASE5:
1063 case BTN_SELECT:
1064 ddoi.dwOfs = DIJOFS_BUTTON(10);
1065 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 10) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
1066 break;
1067 }
1068 sprintf(ddoi.tszName, "%d-Button", i);
1069 _dump_OBJECTINSTANCEA(&ddoi);
1070 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) {
1071 /* return to unaquired state if that's where we were */
1072 if (xfd == -1)
1073 IDirectInputDevice8_Unacquire(iface);
1074 return DI_OK;
1075 }
1076 }
1077 }
1078
1079 /* return to unaquired state if that's where we were */
1080 if (xfd == -1)
1081 IDirectInputDevice8_Unacquire(iface);
1082
1083 return DI_OK;
1084 }
1085
1086 static HRESULT WINAPI JoystickWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
1087 LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback,
1088 LPVOID lpvRef,
1089 DWORD dwFlags)
1090 {
1091 JoystickImpl *This = (JoystickImpl *)iface;
1092
1093 device_enumobjects_AtoWcb_data data;
1094
1095 data.lpCallBack = lpCallback;
1096 data.lpvRef = lpvRef;
1097
1098 return JoystickAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
1099 }
1100
1101 /******************************************************************************
1102 * GetProperty : get input device properties
1103 */
1104 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
1105 REFGUID rguid,
1106 LPDIPROPHEADER pdiph)
1107 {
1108 JoystickImpl *This = (JoystickImpl *)iface;
1109
1110 TRACE("(this=%p,%s,%p)\n",
1111 iface, debugstr_guid(rguid), pdiph);
1112
1113 if (TRACE_ON(dinput))
1114 _dump_DIPROPHEADER(pdiph);
1115
1116 if (!HIWORD(rguid)) {
1117 switch (LOWORD(rguid)) {
1118 case (DWORD) DIPROP_BUFFERSIZE: {
1119 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1120
1121 TRACE(" return buffersize = %d\n",This->queue_len);
1122 pd->dwData = This->queue_len;
1123 break;
1124 }
1125
1126 case (DWORD) DIPROP_RANGE: {
1127 /* LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph; */
1128 if ((pdiph->dwHow == DIPH_BYID) &&
1129 (pdiph->dwObj & DIDFT_ABSAXIS)) {
1130 /* The app is querying the current range of the axis : return the lMin and lMax values */
1131 FIXME("unimplemented axis range query.\n");
1132 }
1133
1134 break;
1135 }
1136
1137 default:
1138 FIXME("Unknown type %p (%s)\n",rguid,debugstr_guid(rguid));
1139 break;
1140 }
1141 }
1142
1143
1144 return DI_OK;
1145 }
1146
1147 /******************************************************************************
1148 * CreateEffect - Create a new FF effect with the specified params
1149 */
1150 static HRESULT WINAPI JoystickAImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface,
1151 REFGUID rguid,
1152 LPCDIEFFECT lpeff,
1153 LPDIRECTINPUTEFFECT *ppdef,
1154 LPUNKNOWN pUnkOuter)
1155 {
1156 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1157 EffectListItem* new = NULL;
1158 HRESULT retval = DI_OK;
1159 #endif
1160
1161 JoystickImpl* This = (JoystickImpl*)iface;
1162 TRACE("(this=%p,%p,%p,%p,%p)\n", This, rguid, lpeff, ppdef, pUnkOuter);
1163
1164 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
1165 TRACE("not available (compiled w/o ff support)\n");
1166 *ppdef = NULL;
1167 return DI_OK;
1168 #else
1169
1170 new = malloc(sizeof(EffectListItem));
1171 new->next = This->top_effect;
1172 This->top_effect = new;
1173
1174 retval = linuxinput_create_effect(&(This->joyfd), rguid, &(new->ref));
1175 if (retval != DI_OK)
1176 return retval;
1177
1178 if (lpeff != NULL)
1179 retval = IDirectInputEffect_SetParameters(new->ref, lpeff, 0);
1180 if (retval != DI_OK && retval != DI_DOWNLOADSKIPPED)
1181 return retval;
1182
1183 *ppdef = new->ref;
1184
1185 if (pUnkOuter != NULL)
1186 FIXME("Interface aggregation not implemented.\n");
1187
1188 return DI_OK;
1189
1190 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */
1191 }
1192
1193 /*******************************************************************************
1194 * EnumEffects - Enumerate available FF effects
1195 */
1196 static HRESULT WINAPI JoystickAImpl_EnumEffects(LPDIRECTINPUTDEVICE8A iface,
1197 LPDIENUMEFFECTSCALLBACKA lpCallback,
1198 LPVOID pvRef,
1199 DWORD dwEffType)
1200 {
1201 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1202 DIEFFECTINFOA dei; /* feif */
1203 DWORD type = DIEFT_GETTYPE(dwEffType);
1204 JoystickImpl* This = (JoystickImpl*)iface;
1205 int xfd = This->joyfd;
1206
1207 TRACE("(this=%p,%p,%ld) type=%ld fd=%d\n", This, pvRef, dwEffType, type, xfd);
1208
1209 dei.dwSize = sizeof(DIEFFECTINFOA);
1210
1211 /* We need to return something even if we're not yet acquired */
1212 if (xfd == -1)
1213 IDirectInputDevice8_Acquire(iface);
1214
1215 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1216 && test_bit(This->ffbits, FF_CONSTANT)) {
1217 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1218 (*lpCallback)(&dei, pvRef);
1219 }
1220
1221 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1222 && test_bit(This->ffbits, FF_PERIODIC)) {
1223 if (test_bit(This->ffbits, FF_SQUARE)) {
1224 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1225 (*lpCallback)(&dei, pvRef);
1226 }
1227 if (test_bit(This->ffbits, FF_SINE)) {
1228 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1229 (*lpCallback)(&dei, pvRef);
1230 }
1231 if (test_bit(This->ffbits, FF_TRIANGLE)) {
1232 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1233 (*lpCallback)(&dei, pvRef);
1234 }
1235 if (test_bit(This->ffbits, FF_SAW_UP)) {
1236 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1237 (*lpCallback)(&dei, pvRef);
1238 }
1239 if (test_bit(This->ffbits, FF_SAW_DOWN)) {
1240 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1241 (*lpCallback)(&dei, pvRef);
1242 }
1243 }
1244
1245 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1246 && test_bit(This->ffbits, FF_RAMP)) {
1247 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1248 (*lpCallback)(&dei, pvRef);
1249 }
1250
1251 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1252 if (test_bit(This->ffbits, FF_SPRING)) {
1253 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1254 (*lpCallback)(&dei, pvRef);
1255 }
1256 if (test_bit(This->ffbits, FF_DAMPER)) {
1257 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1258 (*lpCallback)(&dei, pvRef);
1259 }
1260 if (test_bit(This->ffbits, FF_INERTIA)) {
1261 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1262 (*lpCallback)(&dei, pvRef);
1263 }
1264 if (test_bit(This->ffbits, FF_FRICTION)) {
1265 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1266 (*lpCallback)(&dei, pvRef);
1267 }
1268 }
1269
1270 /* return to unaquired state if that's where it was */
1271 if (xfd == -1)
1272 IDirectInputDevice8_Unacquire(iface);
1273 #endif
1274
1275 return DI_OK;
1276 }
1277
1278 static HRESULT WINAPI JoystickWImpl_EnumEffects(LPDIRECTINPUTDEVICE8W iface,
1279 LPDIENUMEFFECTSCALLBACKW lpCallback,
1280 LPVOID pvRef,
1281 DWORD dwEffType)
1282 {
1283 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1284 /* seems silly to duplicate all this code but all the structures and functions
1285 * are actually different (A/W) */
1286 DIEFFECTINFOW dei; /* feif */
1287 DWORD type = DIEFT_GETTYPE(dwEffType);
1288 JoystickImpl* This = (JoystickImpl*)iface;
1289 int xfd = This->joyfd;
1290
1291 TRACE("(this=%p,%p,%ld) type=%ld fd=%d\n", This, pvRef, dwEffType, type, xfd);
1292
1293 dei.dwSize = sizeof(DIEFFECTINFOW);
1294
1295 /* We need to return something even if we're not yet acquired */
1296 if (xfd == -1)
1297 IDirectInputDevice8_Acquire(iface);
1298
1299 if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1300 && test_bit(This->ffbits, FF_CONSTANT)) {
1301 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1302 (*lpCallback)(&dei, pvRef);
1303 }
1304
1305 if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1306 && test_bit(This->ffbits, FF_PERIODIC)) {
1307 if (test_bit(This->ffbits, FF_SQUARE)) {
1308 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1309 (*lpCallback)(&dei, pvRef);
1310 }
1311 if (test_bit(This->ffbits, FF_SINE)) {
1312 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1313 (*lpCallback)(&dei, pvRef);
1314 }
1315 if (test_bit(This->ffbits, FF_TRIANGLE)) {
1316 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1317 (*lpCallback)(&dei, pvRef);
1318 }
1319 if (test_bit(This->ffbits, FF_SAW_UP)) {
1320 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1321 (*lpCallback)(&dei, pvRef);
1322 }
1323 if (test_bit(This->ffbits, FF_SAW_DOWN)) {
1324 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1325 (*lpCallback)(&dei, pvRef);
1326 }
1327 }
1328
1329 if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1330 && test_bit(This->ffbits, FF_RAMP)) {
1331 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1332 (*lpCallback)(&dei, pvRef);
1333 }
1334
1335 if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1336 if (test_bit(This->ffbits, FF_SPRING)) {
1337 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1338 (*lpCallback)(&dei, pvRef);
1339 }
1340 if (test_bit(This->ffbits, FF_DAMPER)) {
1341 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1342 (*lpCallback)(&dei, pvRef);
1343 }
1344 if (test_bit(This->ffbits, FF_INERTIA)) {
1345 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1346 (*lpCallback)(&dei, pvRef);
1347 }
1348 if (test_bit(This->ffbits, FF_FRICTION)) {
1349 IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1350 (*lpCallback)(&dei, pvRef);
1351 }
1352 }
1353
1354 /* return to unaquired state if that's where it was */
1355 if (xfd == -1)
1356 IDirectInputDevice8_Unacquire(iface);
1357 #endif
1358
1359 return DI_OK;
1360 }
1361
1362 /*******************************************************************************
1363 * GetEffectInfo - Get information about a particular effect
1364 */
1365 static HRESULT WINAPI JoystickAImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8A iface,
1366 LPDIEFFECTINFOA pdei,
1367 REFGUID guid)
1368 {
1369 JoystickImpl* This = (JoystickImpl*)iface;
1370
1371 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1372
1373 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1374 return linuxinput_get_info_A(This->joyfd, guid, pdei);
1375 #else
1376 return DI_OK;
1377 #endif
1378 }
1379
1380 static HRESULT WINAPI JoystickWImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8W iface,
1381 LPDIEFFECTINFOW pdei,
1382 REFGUID guid)
1383 {
1384 JoystickImpl* This = (JoystickImpl*)iface;
1385
1386 TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1387
1388 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1389 return linuxinput_get_info_W(This->joyfd, guid, pdei);
1390 #else
1391 return DI_OK;
1392 #endif
1393 }
1394
1395 /*******************************************************************************
1396 * GetForceFeedbackState - Get information about the device's FF state
1397 */
1398 static HRESULT WINAPI JoystickAImpl_GetForceFeedbackState(
1399 LPDIRECTINPUTDEVICE8A iface,
1400 LPDWORD pdwOut)
1401 {
1402 JoystickImpl* This = (JoystickImpl*)iface;
1403
1404 TRACE("(this=%p,%p)\n", This, pdwOut);
1405
1406 (*pdwOut) = 0;
1407
1408 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1409 /* DIGFFS_STOPPED is the only mandatory flag to report */
1410 if (This->ff_state == FF_STATUS_STOPPED)
1411 (*pdwOut) |= DIGFFS_STOPPED;
1412 #endif
1413
1414 return DI_OK;
1415 }
1416
1417 /*******************************************************************************
1418 * SendForceFeedbackCommand - Send a command to the device's FF system
1419 */
1420 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(
1421 LPDIRECTINPUTDEVICE8A iface,
1422 DWORD dwFlags)
1423 {
1424 JoystickImpl* This = (JoystickImpl*)iface;
1425 TRACE("(this=%p,%ld)\n", This, dwFlags);
1426
1427 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1428 if (dwFlags == DISFFC_STOPALL) {
1429 /* Stop all effects */
1430 EffectListItem* itr = This->top_effect;
1431 while (itr) {
1432 IDirectInputEffect_Stop(itr->ref);
1433 itr = itr->next;
1434 }
1435 } else if (dwFlags == DISFFC_RESET) {
1436 /* Stop, unload, release and free all effects */
1437 /* This returns the device to its "bare" state */
1438 while (This->top_effect) {
1439 EffectListItem* temp = This->top_effect;
1440 IDirectInputEffect_Stop(temp->ref);
1441 IDirectInputEffect_Unload(temp->ref);
1442 IDirectInputEffect_Release(temp->ref);
1443 This->top_effect = temp->next;
1444 free(temp);
1445 }
1446 } else if (dwFlags == DISFFC_PAUSE || dwFlags == DISFFC_CONTINUE) {
1447 FIXME("No support for Pause or Continue in linux\n");
1448 } else if (dwFlags == DISFFC_SETACTUATORSOFF
1449 || dwFlags == DISFFC_SETACTUATORSON) {
1450 FIXME("No direct actuator control in linux\n");
1451 } else {
1452 FIXME("Unknown Force Feedback Command!\n");
1453 return DIERR_INVALIDPARAM;
1454 }
1455 return DI_OK;
1456 #else
1457 return DIERR_UNSUPPORTED;
1458 #endif
1459 }
1460
1461 /*******************************************************************************
1462 * EnumCreatedEffectObjects - Enumerate all the effects that have been
1463 * created for this device.
1464 */
1465 static HRESULT WINAPI JoystickAImpl_EnumCreatedEffectObjects(
1466 LPDIRECTINPUTDEVICE8A iface,
1467 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1468 LPVOID pvRef,
1469 DWORD dwFlags)
1470 {
1471 /* this function is safe to call on non-ff-enabled builds */
1472
1473 JoystickImpl* This = (JoystickImpl*)iface;
1474 EffectListItem* itr = This->top_effect;
1475 TRACE("(this=%p,%p,%p,%ld)\n", This, lpCallback, pvRef, dwFlags);
1476
1477 if (!lpCallback)
1478 return DIERR_INVALIDPARAM;
1479
1480 if (dwFlags != 0)
1481 FIXME("Flags specified, but no flags exist yet (DX9)!");
1482
1483 while (itr) {
1484 (*lpCallback)(itr->ref, pvRef);
1485 itr = itr->next;
1486 }
1487
1488 return DI_OK;
1489 }
1490
1491 static const IDirectInputDevice8AVtbl JoystickAvt =
1492 {
1493 IDirectInputDevice2AImpl_QueryInterface,
1494 IDirectInputDevice2AImpl_AddRef,
1495 JoystickAImpl_Release,
1496 JoystickAImpl_GetCapabilities,
1497 JoystickAImpl_EnumObjects,
1498 JoystickAImpl_GetProperty,
1499 JoystickAImpl_SetProperty,
1500 JoystickAImpl_Acquire,
1501 JoystickAImpl_Unacquire,
1502 JoystickAImpl_GetDeviceState,
1503 JoystickAImpl_GetDeviceData,
1504 JoystickAImpl_SetDataFormat,
1505 JoystickAImpl_SetEventNotification,
1506 IDirectInputDevice2AImpl_SetCooperativeLevel,
1507 IDirectInputDevice2AImpl_GetObjectInfo,
1508 IDirectInputDevice2AImpl_GetDeviceInfo,
1509 IDirectInputDevice2AImpl_RunControlPanel,
1510 IDirectInputDevice2AImpl_Initialize,
1511 JoystickAImpl_CreateEffect,
1512 JoystickAImpl_EnumEffects,
1513 JoystickAImpl_GetEffectInfo,
1514 JoystickAImpl_GetForceFeedbackState,
1515 JoystickAImpl_SendForceFeedbackCommand,
1516 JoystickAImpl_EnumCreatedEffectObjects,
1517 IDirectInputDevice2AImpl_Escape,
1518 JoystickAImpl_Poll,
1519 IDirectInputDevice2AImpl_SendDeviceData,
1520 IDirectInputDevice7AImpl_EnumEffectsInFile,
1521 IDirectInputDevice7AImpl_WriteEffectToFile,
1522 IDirectInputDevice8AImpl_BuildActionMap,
1523 IDirectInputDevice8AImpl_SetActionMap,
1524 IDirectInputDevice8AImpl_GetImageInfo
1525 };
1526
1527 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1528 # define XCAST(fun) (typeof(JoystickWvt.fun))
1529 #else
1530 # define XCAST(fun) (void*)
1531 #endif
1532
1533 static const IDirectInputDevice8WVtbl JoystickWvt =
1534 {
1535 IDirectInputDevice2WImpl_QueryInterface,
1536 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1537 XCAST(Release)JoystickAImpl_Release,
1538 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1539 JoystickWImpl_EnumObjects,
1540 XCAST(GetProperty)JoystickAImpl_GetProperty,
1541 XCAST(SetProperty)JoystickAImpl_SetProperty,
1542 XCAST(Acquire)JoystickAImpl_Acquire,
1543 XCAST(Unacquire)JoystickAImpl_Unacquire,
1544 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1545 XCAST(GetDeviceData)JoystickAImpl_GetDeviceData,
1546 XCAST(SetDataFormat)JoystickAImpl_SetDataFormat,
1547 XCAST(SetEventNotification)JoystickAImpl_SetEventNotification,
1548 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1549 IDirectInputDevice2WImpl_GetObjectInfo,
1550 IDirectInputDevice2WImpl_GetDeviceInfo,
1551 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1552 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1553 XCAST(CreateEffect)JoystickAImpl_CreateEffect,
1554 JoystickWImpl_EnumEffects,
1555 JoystickWImpl_GetEffectInfo,
1556 XCAST(GetForceFeedbackState)JoystickAImpl_GetForceFeedbackState,
1557 XCAST(SendForceFeedbackCommand)JoystickAImpl_SendForceFeedbackCommand,
1558 XCAST(EnumCreatedEffectObjects)JoystickAImpl_EnumCreatedEffectObjects,
1559 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1560 XCAST(Poll)JoystickAImpl_Poll,
1561 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1562 IDirectInputDevice7WImpl_EnumEffectsInFile,
1563 IDirectInputDevice7WImpl_WriteEffectToFile,
1564 IDirectInputDevice8WImpl_BuildActionMap,
1565 IDirectInputDevice8WImpl_SetActionMap,
1566 IDirectInputDevice8WImpl_GetImageInfo
1567 };
1568 #undef XCAST
1569
1570 #else /* HAVE_CORRECT_LINUXINPUT_H */
1571
1572 const struct dinput_device joystick_linuxinput_device = {
1573 "Wine Linux-input joystick driver",
1574 NULL,
1575 NULL,
1576 NULL,
1577 NULL
1578 };
1579
1580 #endif /* HAVE_CORRECT_LINUXINPUT_H */