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_SYS_ERRNO_H
43 # include <sys/errno.h>
44 #endif
45 #ifdef HAVE_LINUX_INPUT_H
46 # include <linux/input.h>
47 # undef SW_MAX
48 # if defined(EVIOCGBIT) && defined(EV_ABS) && defined(BTN_PINKIE)
49 # define HAVE_CORRECT_LINUXINPUT_H
50 # endif
51 #endif
52 #ifdef HAVE_SYS_POLL_H
53 # include <sys/poll.h>
54 #endif
55
56 #include "wine/debug.h"
57 #include "wine/unicode.h"
58 #include "wine/list.h"
59 #include "windef.h"
60 #include "winbase.h"
61 #include "winerror.h"
62 #include "winreg.h"
63 #include "dinput.h"
64
65 #include "dinput_private.h"
66 #include "device_private.h"
67
68 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
69
70
71 /*
72 * Maps POV x & y event values to a DX "clock" position:
73 * 0
74 * 31500 4500
75 * 27000 -1 9000
76 * 22500 13500
77 * 18000
78 */
79 DWORD joystick_map_pov(POINTL *p)
80 {
81 if (p->x > 0)
82 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
83 else if (p->x < 0)
84 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
85 else
86 return p->y < 0 ? 0 : !p->y ? -1 : 18000;
87 }
88
89 /*
90 * This maps the read value (from the input event) to a value in the
91 * 'wanted' range.
92 * Notes:
93 * Dead zone is in % multiplied by a 100 (range 0..10000)
94 */
95 LONG joystick_map_axis(ObjProps *props, int val)
96 {
97 LONG ret;
98 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
99 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
100
101 /* Center input */
102 val -= (props->lDevMin + props->lDevMax) / 2;
103
104 /* Remove dead zone */
105 if (abs( val ) <= dead_zone / 2)
106 val = 0;
107 else
108 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
109
110 /* Scale and map the value from the device range into the required range */
111 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
112 (props->lMin + props->lMax) / 2;
113
114 /* Clamp in case or rounding errors */
115 if (ret > props->lMax) ret = props->lMax;
116 else if (ret < props->lMin) ret = props->lMin;
117
118 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
119 props->lDevMin, dead_zone, props->lDevMax,
120 props->lMin, props->lDeadZone, props->lMax,
121 val, ret );
122
123 return ret;
124 }
125
126 #ifdef HAVE_CORRECT_LINUXINPUT_H
127
128 #define EVDEVPREFIX "/dev/input/event"
129
130 /* Wine joystick driver object instances */
131 #define WINE_JOYSTICK_MAX_AXES 8
132 #define WINE_JOYSTICK_MAX_POVS 4
133 #define WINE_JOYSTICK_MAX_BUTTONS 128
134
135 struct wine_input_absinfo {
136 LONG value;
137 LONG minimum;
138 LONG maximum;
139 LONG fuzz;
140 LONG flat;
141 };
142
143 /* implemented in effect_linuxinput.c */
144 HRESULT linuxinput_create_effect(int* fd, REFGUID rguid, struct list *parent_list_entry, LPDIRECTINPUTEFFECT* peff);
145 HRESULT linuxinput_get_info_A(int fd, REFGUID rguid, LPDIEFFECTINFOA info);
146 HRESULT linuxinput_get_info_W(int fd, REFGUID rguid, LPDIEFFECTINFOW info);
147
148 typedef struct JoystickImpl JoystickImpl;
149 static const IDirectInputDevice8AVtbl JoystickAvt;
150 static const IDirectInputDevice8WVtbl JoystickWvt;
151
152 struct JoyDev {
153 char *device;
154 char *name;
155 GUID guid;
156
157 int has_ff;
158 int num_effects;
159
160 /* data returned by EVIOCGBIT for caps, EV_ABS, EV_KEY, and EV_FF */
161 BYTE evbits[(EV_MAX+7)/8];
162 BYTE absbits[(ABS_MAX+7)/8];
163 BYTE keybits[(KEY_MAX+7)/8];
164 BYTE ffbits[(FF_MAX+7)/8];
165
166 /* data returned by the EVIOCGABS() ioctl */
167 struct wine_input_absinfo axes[ABS_MAX];
168 };
169
170 struct JoystickImpl
171 {
172 struct IDirectInputDevice2AImpl base;
173
174 struct JoyDev *joydev;
175
176 /* joystick private */
177 int joyfd;
178
179 DIJOYSTATE2 js;
180
181 ObjProps props[ABS_MAX];
182
183 int axes[ABS_MAX];
184 POINTL povs[4];
185
186 /* LUT for KEY_ to offset in rgbButtons */
187 BYTE buttons[KEY_MAX];
188
189 DWORD numAxes;
190 DWORD numPOVs;
191 DWORD numButtons;
192
193 /* Force feedback variables */
194 struct list ff_effects;
195 int ff_state;
196 };
197
198 static void fake_current_js_state(JoystickImpl *ji);
199 static void find_joydevs(void);
200
201 /* This GUID is slightly different from the linux joystick one. Take note. */
202 static const GUID DInput_Wine_Joystick_Base_GUID = { /* 9e573eda-7734-11d2-8d4a-23903fb6bdf7 */
203 0x9e573eda,
204 0x7734,
205 0x11d2,
206 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
207 };
208
209 #define test_bit(arr,bit) (((BYTE*)(arr))[(bit)>>3]&(1<<((bit)&7)))
210
211 #define MAX_JOYDEV 64
212
213 static int have_joydevs = -1;
214 static struct JoyDev *joydevs = NULL;
215
216 static void find_joydevs(void)
217 {
218 int i;
219
220 if (have_joydevs!=-1) {
221 return;
222 }
223
224 have_joydevs = 0;
225
226 for (i=0;i<MAX_JOYDEV;i++) {
227 char buf[MAX_PATH];
228 struct JoyDev joydev = {0};
229 int fd;
230 int no_ff_check = 0;
231 int j;
232
233 snprintf(buf,MAX_PATH,EVDEVPREFIX"%d",i);
234 buf[MAX_PATH-1] = 0;
235
236 if ((fd=open(buf, O_RDWR))==-1) {
237 fd = open(buf, O_RDONLY);
238 no_ff_check = 1;
239 }
240
241 if (fd!=-1) {
242
243 if ((-1==ioctl(fd,EVIOCGBIT(0,sizeof(joydev.evbits)),joydev.evbits))) {
244 perror("EVIOCGBIT 0");
245 close(fd);
246 continue;
247 }
248 if (-1==ioctl(fd,EVIOCGBIT(EV_ABS,sizeof(joydev.absbits)),joydev.absbits)) {
249 perror("EVIOCGBIT EV_ABS");
250 close(fd);
251 continue;
252 }
253 if (-1==ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(joydev.keybits)),joydev.keybits)) {
254 perror("EVIOCGBIT EV_KEY");
255 close(fd);
256 continue;
257 }
258 /* A true joystick has at least axis X and Y, and at least 1
259 * button. copied from linux/drivers/input/joydev.c */
260 if (test_bit(joydev.absbits,ABS_X) && test_bit(joydev.absbits,ABS_Y) &&
261 ( test_bit(joydev.keybits,BTN_TRIGGER) ||
262 test_bit(joydev.keybits,BTN_A) ||
263 test_bit(joydev.keybits,BTN_1)
264 )
265 ) {
266
267 joydev.device = strdup(buf);
268
269 if (-1!=ioctl(fd, EVIOCGNAME(MAX_PATH-1), buf)) {
270 buf[MAX_PATH-1] = 0;
271 joydev.name = strdup(buf);
272 } else {
273 joydev.name = joydev.device;
274 }
275
276 joydev.guid = DInput_Wine_Joystick_Base_GUID;
277 joydev.guid.Data3 += have_joydevs;
278
279 TRACE("Found a joystick on %s: %s (%s)\n",
280 joydev.device, joydev.name,
281 debugstr_guid(&joydev.guid)
282 );
283
284 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
285 if (!no_ff_check &&
286 test_bit(joydev.evbits, EV_FF) &&
287 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(joydev.ffbits)), joydev.ffbits) != -1 &&
288 ioctl(fd, EVIOCGEFFECTS, &joydev.num_effects) != -1 &&
289 joydev.num_effects > 0)
290 {
291 TRACE(" ... with force feedback\n");
292 joydev.has_ff = 1;
293 }
294 #endif
295
296 for (j=0;j<ABS_MAX;j++) {
297 if (test_bit(joydev.absbits,j)) {
298 if (-1!=ioctl(fd,EVIOCGABS(j),&(joydev.axes[j]))) {
299 TRACE(" ... with axis %d: cur=%d, min=%d, max=%d, fuzz=%d, flat=%d\n",
300 j,
301 joydev.axes[j].value,
302 joydev.axes[j].minimum,
303 joydev.axes[j].maximum,
304 joydev.axes[j].fuzz,
305 joydev.axes[j].flat
306 );
307 }
308 }
309 }
310
311 if (have_joydevs==0) {
312 joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
313 } else {
314 HeapReAlloc(GetProcessHeap(), 0, joydevs, (1+have_joydevs) * sizeof(struct JoyDev));
315 }
316 memcpy(joydevs+have_joydevs, &joydev, sizeof(struct JoyDev));
317 have_joydevs++;
318 }
319
320 close(fd);
321 }
322 }
323 }
324
325 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
326 {
327 DWORD dwSize = lpddi->dwSize;
328
329 TRACE("%d %p\n", dwSize, lpddi);
330 memset(lpddi, 0, dwSize);
331
332 lpddi->dwSize = dwSize;
333 lpddi->guidInstance = joydevs[id].guid;
334 lpddi->guidProduct = DInput_Wine_Joystick_Base_GUID;
335 lpddi->guidFFDriver = GUID_NULL;
336
337 if (version >= 0x0800)
338 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
339 else
340 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
341
342 strcpy(lpddi->tszInstanceName, joydevs[id].name);
343 strcpy(lpddi->tszProductName, joydevs[id].device);
344 }
345
346 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
347 {
348 DWORD dwSize = lpddi->dwSize;
349
350 TRACE("%d %p\n", dwSize, lpddi);
351 memset(lpddi, 0, dwSize);
352
353 lpddi->dwSize = dwSize;
354 lpddi->guidInstance = joydevs[id].guid;
355 lpddi->guidProduct = DInput_Wine_Joystick_Base_GUID;
356 lpddi->guidFFDriver = GUID_NULL;
357
358 if (version >= 0x0800)
359 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
360 else
361 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
362
363 MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
364 MultiByteToWideChar(CP_ACP, 0, joydevs[id].device, -1, lpddi->tszProductName, MAX_PATH);
365 }
366
367 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
368 {
369 find_joydevs();
370
371 if (id >= have_joydevs) {
372 return FALSE;
373 }
374
375 if (!((dwDevType == 0) ||
376 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
377 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
378 return FALSE;
379
380 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
381 if (dwFlags & DIEDFL_FORCEFEEDBACK)
382 return FALSE;
383 #endif
384
385 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
386 fill_joystick_dideviceinstanceA(lpddi, version, id);
387 return TRUE;
388 }
389 return FALSE;
390 }
391
392 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
393 {
394 find_joydevs();
395
396 if (id >= have_joydevs) {
397 return FALSE;
398 }
399
400 if (!((dwDevType == 0) ||
401 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
402 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
403 return FALSE;
404
405 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
406 if (dwFlags & DIEDFL_FORCEFEEDBACK)
407 return FALSE;
408 #endif
409
410 if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
411 fill_joystick_dideviceinstanceW(lpddi, version, id);
412 return TRUE;
413 }
414 return FALSE;
415 }
416
417 static JoystickImpl *alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput, unsigned short index)
418 {
419 JoystickImpl* newDevice;
420 LPDIDATAFORMAT df = NULL;
421 int i, idx = 0;
422 char buffer[MAX_PATH+16];
423 HKEY hkey, appkey;
424 LONG def_deadzone = 0;
425
426 newDevice = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(JoystickImpl));
427 if (!newDevice) return NULL;
428
429 newDevice->base.lpVtbl = jvt;
430 newDevice->base.ref = 1;
431 newDevice->base.guid = *rguid;
432 newDevice->base.dinput = dinput;
433 newDevice->joyfd = -1;
434 newDevice->joydev = &joydevs[index];
435 list_init(&newDevice->ff_effects);
436 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
437 newDevice->ff_state = FF_STATUS_STOPPED;
438 #endif
439 InitializeCriticalSection(&newDevice->base.crit);
440 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
441
442 /* get options */
443 get_app_key(&hkey, &appkey);
444
445 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH))
446 {
447 def_deadzone = atoi(buffer);
448 TRACE("setting default deadzone to: %d\n", def_deadzone);
449 }
450 if (appkey) RegCloseKey(appkey);
451 if (hkey) RegCloseKey(hkey);
452
453 /* Create copy of default data format */
454 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto failed;
455 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
456 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
457
458 /* Supported Axis & POVs should map 1-to-1 */
459 for (i = 0; i < WINE_JOYSTICK_MAX_AXES; i++)
460 {
461 if (!test_bit(newDevice->joydev->absbits, i)) {
462 newDevice->axes[i] = -1;
463 continue;
464 }
465
466 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i], df->dwObjSize);
467 newDevice->axes[i] = idx;
468 newDevice->props[idx].lDevMin = newDevice->joydev->axes[i].minimum;
469 newDevice->props[idx].lDevMax = newDevice->joydev->axes[i].maximum;
470 newDevice->props[idx].lMin = 0;
471 newDevice->props[idx].lMax = 0xffff;
472 newDevice->props[idx].lSaturation = 0;
473 newDevice->props[idx].lDeadZone = def_deadzone;
474
475 /* Linux supports force-feedback on X & Y axes only */
476 if (newDevice->joydev->has_ff && (i == 0 || i == 1))
477 df->rgodf[idx].dwFlags |= DIDOI_FFACTUATOR;
478
479 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->numAxes++) | DIDFT_ABSAXIS;
480 }
481
482 for (i = 0; i < WINE_JOYSTICK_MAX_POVS; i++)
483 {
484 if (!test_bit(newDevice->joydev->absbits, ABS_HAT0X + i * 2) ||
485 !test_bit(newDevice->joydev->absbits, ABS_HAT0Y + i * 2)) {
486 newDevice->axes[ABS_HAT0X + i * 2] = newDevice->axes[ABS_HAT0Y + i * 2] = -1;
487 continue;
488 }
489
490 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + WINE_JOYSTICK_MAX_AXES], df->dwObjSize);
491 newDevice->axes[ABS_HAT0X + i * 2] = newDevice->axes[ABS_HAT0Y + i * 2] = i;
492 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->numPOVs++) | DIDFT_POV;
493 }
494
495 /* Buttons can be anywhere, so check all */
496 for (i = 0; i < KEY_MAX && newDevice->numButtons < WINE_JOYSTICK_MAX_BUTTONS; i++)
497 {
498 if (!test_bit(newDevice->joydev->keybits, i)) continue;
499
500 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[newDevice->numButtons + WINE_JOYSTICK_MAX_AXES + WINE_JOYSTICK_MAX_POVS], df->dwObjSize);
501 newDevice->buttons[i] = 0x80 | newDevice->numButtons;
502 df->rgodf[idx ].pguid = &GUID_Button;
503 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->numButtons++) | DIDFT_PSHBUTTON;
504 }
505 df->dwNumObjs = idx;
506
507 fake_current_js_state(newDevice);
508
509 newDevice->base.data_format.wine_df = df;
510 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
511 return newDevice;
512
513 failed:
514 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
515 HeapFree(GetProcessHeap(), 0, df);
516 HeapFree(GetProcessHeap(), 0, newDevice);
517 return NULL;
518 }
519
520 /******************************************************************************
521 * get_joystick_index : Get the joystick index from a given GUID
522 */
523 static unsigned short get_joystick_index(REFGUID guid)
524 {
525 GUID wine_joystick = DInput_Wine_Joystick_Base_GUID;
526 GUID dev_guid = *guid;
527
528 wine_joystick.Data3 = 0;
529 dev_guid.Data3 = 0;
530
531 /* for the standard joystick GUID use index 0 */
532 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
533
534 /* for the wine joystick GUIDs use the index stored in Data3 */
535 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3 - DInput_Wine_Joystick_Base_GUID.Data3;
536
537 return MAX_JOYDEV;
538 }
539
540 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
541 {
542 unsigned short index;
543
544 find_joydevs();
545
546 if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
547 have_joydevs && index < have_joydevs)
548 {
549 if ((riid == NULL) ||
550 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
551 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
552 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
553 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
554 {
555 *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &JoystickAvt, dinput, index);
556 TRACE("Created a Joystick device (%p)\n", *pdev);
557
558 if (*pdev == NULL)
559 {
560 ERR("out of memory\n");
561 return DIERR_OUTOFMEMORY;
562 }
563 return DI_OK;
564 }
565
566 WARN("no interface\n");
567 return DIERR_NOINTERFACE;
568 }
569
570 return DIERR_DEVICENOTREG;
571 }
572
573
574 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
575 {
576 unsigned short index;
577
578 find_joydevs();
579
580 if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
581 have_joydevs && index < have_joydevs)
582 {
583 if ((riid == NULL) ||
584 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
585 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
586 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
587 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
588 {
589 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &JoystickWvt, dinput, index);
590 TRACE("Created a Joystick device (%p)\n", *pdev);
591
592 if (*pdev == NULL)
593 {
594 ERR("out of memory\n");
595 return DIERR_OUTOFMEMORY;
596 }
597 return DI_OK;
598 }
599 WARN("no interface\n");
600 return DIERR_NOINTERFACE;
601 }
602
603 WARN("invalid device GUID\n");
604 return DIERR_DEVICENOTREG;
605 }
606
607 const struct dinput_device joystick_linuxinput_device = {
608 "Wine Linux-input joystick driver",
609 joydev_enum_deviceA,
610 joydev_enum_deviceW,
611 joydev_create_deviceA,
612 joydev_create_deviceW
613 };
614
615 /******************************************************************************
616 * Acquire : gets exclusive control of the joystick
617 */
618 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
619 {
620 JoystickImpl *This = (JoystickImpl *)iface;
621 HRESULT res;
622
623 TRACE("(this=%p)\n",This);
624
625 res = IDirectInputDevice2AImpl_Acquire(iface);
626 if (res==DI_OK) {
627 if (-1==(This->joyfd=open(This->joydev->device,O_RDWR))) {
628 if (-1==(This->joyfd=open(This->joydev->device,O_RDONLY))) {
629 /* Couldn't open the device at all */
630 perror(This->joydev->device);
631 IDirectInputDevice2AImpl_Unacquire(iface);
632 return DIERR_NOTFOUND;
633 } else {
634 /* Couldn't open in r/w but opened in read-only. */
635 WARN("Could not open %s in read-write mode. Force feedback will be disabled.\n", This->joydev->device);
636 }
637 }
638 }
639 else
640 WARN("Failed to acquire: %x\n", res);
641
642 return res;
643 }
644
645 /******************************************************************************
646 * Unacquire : frees the joystick
647 */
648 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
649 {
650 JoystickImpl *This = (JoystickImpl *)iface;
651 HRESULT res;
652
653 TRACE("(this=%p)\n",This);
654 res = IDirectInputDevice2AImpl_Unacquire(iface);
655 if (res==DI_OK && This->joyfd!=-1) {
656 close(This->joyfd);
657 This->joyfd = -1;
658 }
659 return res;
660 }
661
662 /*
663 * set the current state of the js device as it would be with the middle
664 * values on the axes
665 */
666 #define CENTER_AXIS(a) \
667 (ji->axes[a] == -1 ? 0 : joystick_map_axis( &ji->props[ji->axes[a]], \
668 ji->joydev->axes[a].value ))
669 static void fake_current_js_state(JoystickImpl *ji)
670 {
671 int i;
672
673 /* center the axes */
674 ji->js.lX = CENTER_AXIS(ABS_X);
675 ji->js.lY = CENTER_AXIS(ABS_Y);
676 ji->js.lZ = CENTER_AXIS(ABS_Z);
677 ji->js.lRx = CENTER_AXIS(ABS_RX);
678 ji->js.lRy = CENTER_AXIS(ABS_RY);
679 ji->js.lRz = CENTER_AXIS(ABS_RZ);
680 ji->js.rglSlider[0] = CENTER_AXIS(ABS_THROTTLE);
681 ji->js.rglSlider[1] = CENTER_AXIS(ABS_RUDDER);
682
683 /* POV center is -1 */
684 for (i = 0; i < 4; i++)
685 ji->js.rgdwPOV[i] = -1;
686 }
687 #undef CENTER_AXIS
688
689 /* convert wine format offset to user format object index */
690 static void joy_polldev(JoystickImpl *This)
691 {
692 struct pollfd plfd;
693 struct input_event ie;
694
695 if (This->joyfd==-1)
696 return;
697
698 while (1)
699 {
700 LONG value = 0;
701 int inst_id = -1;
702
703 plfd.fd = This->joyfd;
704 plfd.events = POLLIN;
705
706 if (poll(&plfd,1,0) != 1)
707 return;
708
709 /* we have one event, so we can read */
710 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
711 return;
712
713 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
714 switch (ie.type) {
715 case EV_KEY: /* button */
716 {
717 int btn = This->buttons[ie.code];
718
719 TRACE("(%p) %d -> %d\n", This, ie.code, btn);
720 if (btn & 0x80)
721 {
722 btn &= 0x7F;
723 inst_id = DIDFT_MAKEINSTANCE(btn) | DIDFT_PSHBUTTON;
724 This->js.rgbButtons[btn] = value = ie.value ? 0x80 : 0x00;
725 }
726 break;
727 }
728 case EV_ABS:
729 {
730 int axis = This->axes[ie.code];
731 if (axis==-1) {
732 break;
733 }
734 inst_id = DIDFT_MAKEINSTANCE(axis) | (ie.code < ABS_HAT0X ? DIDFT_ABSAXIS : DIDFT_POV);
735 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], ie.value);
736
737 switch (ie.code) {
738 case ABS_X: This->js.lX = value; break;
739 case ABS_Y: This->js.lY = value; break;
740 case ABS_Z: This->js.lZ = value; break;
741 case ABS_RX: This->js.lRx = value; break;
742 case ABS_RY: This->js.lRy = value; break;
743 case ABS_RZ: This->js.lRz = value; break;
744 case ABS_THROTTLE: This->js.rglSlider[0] = value; break;
745 case ABS_RUDDER: This->js.rglSlider[1] = value; break;
746 case ABS_HAT0X: case ABS_HAT0Y: case ABS_HAT1X: case ABS_HAT1Y:
747 case ABS_HAT2X: case ABS_HAT2Y: case ABS_HAT3X: case ABS_HAT3Y:
748 {
749 int idx = (ie.code - ABS_HAT0X) / 2;
750
751 if (ie.code % 2)
752 This->povs[idx].y = ie.value;
753 else
754 This->povs[idx].x = ie.value;
755
756 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
757 break;
758 }
759 default:
760 FIXME("unhandled joystick axis event (code %d, value %d)\n",ie.code,ie.value);
761 }
762 break;
763 }
764 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
765 case EV_FF_STATUS:
766 This->ff_state = ie.value;
767 break;
768 #endif
769 #ifdef EV_SYN
770 case EV_SYN:
771 /* there is nothing to do */
772 break;
773 #endif
774 default:
775 FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
776 break;
777 }
778 if (inst_id >= 0)
779 queue_event((LPDIRECTINPUTDEVICE8A)This,
780 id_to_offset(&This->base.data_format, inst_id),
781 value, ie.time.tv_usec, This->base.dinput->evsequence++);
782 }
783 }
784
785 /******************************************************************************
786 * GetDeviceState : returns the "state" of the joystick.
787 *
788 */
789 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
790 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
791 ) {
792 JoystickImpl *This = (JoystickImpl *)iface;
793
794 TRACE("(this=%p,0x%08x,%p)\n", This, len, ptr);
795
796 if (!This->base.acquired)
797 {
798 WARN("not acquired\n");
799 return DIERR_NOTACQUIRED;
800 }
801
802 joy_polldev(This);
803
804 /* convert and copy data to user supplied buffer */
805 fill_DataFormat(ptr, &This->js, &This->base.data_format);
806
807 return DI_OK;
808 }
809
810 /******************************************************************************
811 * SetProperty : change input device properties
812 */
813 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface,
814 REFGUID rguid,
815 LPCDIPROPHEADER ph)
816 {
817 JoystickImpl *This = (JoystickImpl *)iface;
818
819 if (!ph) {
820 WARN("invalid argument\n");
821 return DIERR_INVALIDPARAM;
822 }
823
824 TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
825 TRACE("ph.dwSize = %d, ph.dwHeaderSize =%d, ph.dwObj = %d, ph.dwHow= %d\n",
826 ph->dwSize, ph->dwHeaderSize, ph->dwObj, ph->dwHow);
827
828 if (!HIWORD(rguid)) {
829 switch (LOWORD(rguid)) {
830 case (DWORD)DIPROP_RANGE: {
831 LPCDIPROPRANGE pr = (LP