[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[reactos.git] / dll / directx / wine / dinput / joystick_linux.c
1 /* DirectInput Joystick device
2 *
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 /*
23 * To Do:
24 * dead zone
25 * force feedback
26 */
27
28 #include "dinput_private.h"
29
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
35 #endif
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
38 #endif
39 #ifdef HAVE_LINUX_IOCTL_H
40 # include <linux/ioctl.h>
41 #endif
42 #ifdef HAVE_LINUX_JOYSTICK_H
43 # include <linux/joystick.h>
44 # undef SW_MAX
45 #endif
46 #ifdef HAVE_SYS_POLL_H
47 # include <sys/poll.h>
48 #endif
49
50 #ifdef HAVE_LINUX_22_JOYSTICK_API
51
52 #define JOYDEV_NEW "/dev/input/js"
53 #define JOYDEV_OLD "/dev/js"
54 #define JOYDEVDRIVER " (js)"
55
56 struct JoyDev
57 {
58 char device[MAX_PATH];
59 char name[MAX_PATH];
60 GUID guid_product;
61
62 BYTE axis_count;
63 BYTE button_count;
64 int *dev_axes_map;
65
66 WORD vendor_id, product_id, bus_type;
67 };
68
69 typedef struct JoystickImpl JoystickImpl;
70 static const IDirectInputDevice8AVtbl JoystickAvt;
71 static const IDirectInputDevice8WVtbl JoystickWvt;
72 struct JoystickImpl
73 {
74 struct JoystickGenericImpl generic;
75
76 struct JoyDev *joydev;
77
78 /* joystick private */
79 int joyfd;
80 POINTL povs[4];
81 };
82
83 static inline JoystickImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
84 {
85 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface),
86 JoystickGenericImpl, base), JoystickImpl, generic);
87 }
88 static inline JoystickImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
89 {
90 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface),
91 JoystickGenericImpl, base), JoystickImpl, generic);
92 }
93
94 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickImpl *This)
95 {
96 return &This->generic.base.IDirectInputDevice8W_iface;
97 }
98
99 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
100 0x9e573ed9,
101 0x7734,
102 0x11d2,
103 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
104 };
105
106 /*
107 * Construct the GUID in the same way of Windows doing this.
108 * Data1 is concatenation of productid and vendorid.
109 * Data2 and Data3 are NULL.
110 * Data4 seems to be a constant.
111 */
112 static const GUID DInput_Wine_Joystick_Constant_Part_GUID = {
113 0x000000000,
114 0x0000,
115 0x0000,
116 {0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44}
117 };
118
119 #define MAX_JOYSTICKS 64
120 static INT joystick_devices_count = -1;
121 static struct JoyDev *joystick_devices;
122
123 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface);
124
125 #define SYS_PATH_FORMAT "/sys/class/input/js%d/device/id/%s"
126 static BOOL read_sys_id_variable(int index, const char *property, WORD *value)
127 {
128 char sys_path[sizeof(SYS_PATH_FORMAT) + 16], id_str[5];
129 int sys_fd;
130 BOOL ret = FALSE;
131
132 sprintf(sys_path, SYS_PATH_FORMAT, index, property);
133 if ((sys_fd = open(sys_path, O_RDONLY)) != -1)
134 {
135 if (read(sys_fd, id_str, 4) == 4)
136 {
137 id_str[4] = '\0';
138 *value = strtol(id_str, NULL, 16);
139 ret = TRUE;
140 }
141
142 close(sys_fd);
143 }
144 return ret;
145 }
146 #undef SYS_PATH_FORMAT
147
148 static INT find_joystick_devices(void)
149 {
150 INT i;
151
152 if (joystick_devices_count != -1) return joystick_devices_count;
153
154 joystick_devices_count = 0;
155 for (i = 0; i < MAX_JOYSTICKS; i++)
156 {
157 int fd;
158 struct JoyDev joydev, *new_joydevs;
159 BYTE axes_map[ABS_MAX + 1];
160
161 snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_NEW, i);
162 if ((fd = open(joydev.device, O_RDONLY)) == -1)
163 {
164 snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_OLD, i);
165 if ((fd = open(joydev.device, O_RDONLY)) == -1) continue;
166 }
167
168 strcpy(joydev.name, "Wine Joystick");
169 #if defined(JSIOCGNAME)
170 if (ioctl(fd, JSIOCGNAME(sizeof(joydev.name) - sizeof(JOYDEVDRIVER)), joydev.name) < 0)
171 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joydev.device, strerror(errno));
172 #endif
173
174 /* Append driver name */
175 strcat(joydev.name, JOYDEVDRIVER);
176
177 if (device_disabled_registry(joydev.name)) {
178 close(fd);
179 continue;
180 }
181
182 #ifdef JSIOCGAXES
183 if (ioctl(fd, JSIOCGAXES, &joydev.axis_count) < 0)
184 {
185 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defaulting to 2\n", joydev.device, strerror(errno));
186 joydev.axis_count = 2;
187 }
188 #else
189 WARN("reading number of joystick axes unsupported in this platform, defaulting to 2\n");
190 joydev.axis_count = 2;
191 #endif
192 #ifdef JSIOCGBUTTONS
193 if (ioctl(fd, JSIOCGBUTTONS, &joydev.button_count) < 0)
194 {
195 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defaulting to 2\n", joydev.device, strerror(errno));
196 joydev.button_count = 2;
197 }
198 #else
199 WARN("reading number of joystick buttons unsupported in this platform, defaulting to 2\n");
200 joydev.button_count = 2;
201 #endif
202
203 if (ioctl(fd, JSIOCGAXMAP, axes_map) < 0)
204 {
205 WARN("ioctl(%s,JSIOCGAXMAP) failed: %s\n", joydev.device, strerror(errno));
206 joydev.dev_axes_map = NULL;
207 }
208 else
209 if ((joydev.dev_axes_map = HeapAlloc(GetProcessHeap(), 0, joydev.axis_count * sizeof(int))))
210 {
211 INT j, found_axes = 0;
212
213 /* Remap to DI numbers */
214 for (j = 0; j < joydev.axis_count; j++)
215 {
216 if (axes_map[j] < 8)
217 {
218 /* Axis match 1-to-1 */
219 joydev.dev_axes_map[j] = j;
220 found_axes++;
221 }
222 else if (axes_map[j] == 16 ||
223 axes_map[j] == 17)
224 {
225 /* POV axis */
226 joydev.dev_axes_map[j] = 8;
227 found_axes++;
228 }
229 else
230 joydev.dev_axes_map[j] = -1;
231 }
232
233 /* If no axes were configured but there are axes assume a 1-to-1 (wii controller) */
234 if (joydev.axis_count && !found_axes)
235 {
236 int axes_limit = min(joydev.axis_count, 8); /* generic driver limit */
237
238 ERR("Incoherent joystick data, advertised %d axes, detected 0. Assuming 1-to-1.\n",
239 joydev.axis_count);
240 for (j = 0; j < axes_limit; j++)
241 joydev.dev_axes_map[j] = j;
242
243 joydev.axis_count = axes_limit;
244 }
245 }
246
247 /* Find vendor_id and product_id in sysfs */
248 joydev.vendor_id = 0;
249 joydev.product_id = 0;
250
251 read_sys_id_variable(i, "vendor", &joydev.vendor_id);
252 read_sys_id_variable(i, "product", &joydev.product_id);
253 read_sys_id_variable(i, "bustype", &joydev.bus_type);
254
255 if (joydev.vendor_id == 0 || joydev.product_id == 0)
256 {
257 joydev.guid_product = DInput_Wine_Joystick_GUID;
258 }
259 else
260 {
261 /* Concatenate product_id with vendor_id to mimic Windows behaviour */
262 joydev.guid_product = DInput_Wine_Joystick_Constant_Part_GUID;
263 joydev.guid_product.Data1 = MAKELONG(joydev.vendor_id, joydev.product_id);
264 }
265
266 close(fd);
267
268 if (!joystick_devices_count)
269 new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
270 else
271 new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joystick_devices,
272 (joystick_devices_count + 1) * sizeof(struct JoyDev));
273 if (!new_joydevs) continue;
274
275 TRACE("Found a joystick on %s: %s\n with %d axes and %d buttons\n", joydev.device,
276 joydev.name, joydev.axis_count, joydev.button_count);
277
278 joystick_devices = new_joydevs;
279 joystick_devices[joystick_devices_count++] = joydev;
280 }
281
282 return joystick_devices_count;
283 }
284
285 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
286 {
287 DWORD dwSize = lpddi->dwSize;
288
289 TRACE("%d %p\n", dwSize, lpddi);
290 memset(lpddi, 0, dwSize);
291
292 /* Return joystick */
293 lpddi->dwSize = dwSize;
294 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
295 lpddi->guidInstance.Data3 = id;
296 lpddi->guidProduct = joystick_devices[id].guid_product;
297 /* we only support traditional joysticks for now */
298 if (version >= 0x0800)
299 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
300 else
301 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
302
303 /* Assume the joystick as HID if it is attached to USB bus and has a valid VID/PID */
304 if (joystick_devices[id].bus_type == BUS_USB &&
305 joystick_devices[id].vendor_id && joystick_devices[id].product_id)
306 {
307 lpddi->dwDevType |= DIDEVTYPE_HID;
308 lpddi->wUsagePage = 0x01; /* Desktop */
309 if (lpddi->dwDevType == DI8DEVTYPE_JOYSTICK || lpddi->dwDevType == DIDEVTYPE_JOYSTICK)
310 lpddi->wUsage = 0x04; /* Joystick */
311 else
312 lpddi->wUsage = 0x05; /* Game Pad */
313 }
314
315 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
316 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszProductName, MAX_PATH);
317 lpddi->guidFFDriver = GUID_NULL;
318 }
319
320 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
321 {
322 DIDEVICEINSTANCEW lpddiW;
323 DWORD dwSize = lpddi->dwSize;
324
325 lpddiW.dwSize = sizeof(lpddiW);
326 fill_joystick_dideviceinstanceW(&lpddiW, version, id);
327
328 TRACE("%d %p\n", dwSize, lpddi);
329 memset(lpddi, 0, dwSize);
330
331 /* Convert W->A */
332 lpddi->dwSize = dwSize;
333 lpddi->guidInstance = lpddiW.guidInstance;
334 lpddi->guidProduct = lpddiW.guidProduct;
335 lpddi->dwDevType = lpddiW.dwDevType;
336 strcpy(lpddi->tszInstanceName, joystick_devices[id].name);
337 strcpy(lpddi->tszProductName, joystick_devices[id].name);
338 lpddi->guidFFDriver = lpddiW.guidFFDriver;
339 lpddi->wUsagePage = lpddiW.wUsagePage;
340 lpddi->wUsage = lpddiW.wUsage;
341 }
342
343 static HRESULT joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
344 {
345 int fd = -1;
346
347 if (id >= find_joystick_devices()) return E_FAIL;
348
349 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
350 WARN("force feedback not supported\n");
351 return S_FALSE;
352 }
353
354 if ((dwDevType == 0) ||
355 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
356 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
357 /* check whether we have a joystick */
358 if ((fd = open(joystick_devices[id].device, O_RDONLY)) == -1)
359 {
360 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
361 return S_FALSE;
362 }
363 fill_joystick_dideviceinstanceA( lpddi, version, id );
364 close(fd);
365 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
366 return S_OK;
367 }
368
369 return S_FALSE;
370 }
371
372 static HRESULT joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
373 {
374 int fd = -1;
375
376 if (id >= find_joystick_devices()) return E_FAIL;
377
378 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
379 WARN("force feedback not supported\n");
380 return S_FALSE;
381 }
382
383 if ((dwDevType == 0) ||
384 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
385 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
386 /* check whether we have a joystick */
387 if ((fd = open(joystick_devices[id].device, O_RDONLY)) == -1)
388 {
389 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
390 return S_FALSE;
391 }
392 fill_joystick_dideviceinstanceW( lpddi, version, id );
393 close(fd);
394 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
395 return S_OK;
396 }
397
398 return S_FALSE;
399 }
400
401 static HRESULT alloc_device(REFGUID rguid, IDirectInputImpl *dinput,
402 JoystickImpl **pdev, unsigned short index)
403 {
404 DWORD i;
405 JoystickImpl* newDevice;
406 HRESULT hr;
407 LPDIDATAFORMAT df = NULL;
408 int idx = 0;
409 DIDEVICEINSTANCEW ddi;
410
411 TRACE("%s %p %p %hu\n", debugstr_guid(rguid), dinput, pdev, index);
412
413 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
414 if (newDevice == 0) {
415 WARN("out of memory\n");
416 *pdev = 0;
417 return DIERR_OUTOFMEMORY;
418 }
419
420 newDevice->joydev = &joystick_devices[index];
421 newDevice->joyfd = -1;
422 newDevice->generic.guidInstance = DInput_Wine_Joystick_GUID;
423 newDevice->generic.guidInstance.Data3 = index;
424 newDevice->generic.guidProduct = DInput_Wine_Joystick_GUID;
425 newDevice->generic.joy_polldev = joy_polldev;
426 newDevice->generic.name = newDevice->joydev->name;
427 newDevice->generic.device_axis_count = newDevice->joydev->axis_count;
428 newDevice->generic.devcaps.dwButtons = newDevice->joydev->button_count;
429
430 if (newDevice->generic.devcaps.dwButtons > 128)
431 {
432 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
433 newDevice->generic.devcaps.dwButtons = 128;
434 }
435
436 newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
437 newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
438 newDevice->generic.base.ref = 1;
439 newDevice->generic.base.dinput = dinput;
440 newDevice->generic.base.guid = *rguid;
441 InitializeCriticalSection(&newDevice->generic.base.crit);
442 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
443
444 /* setup_dinput_options may change these */
445 newDevice->generic.deadzone = 0;
446
447 /* do any user specified configuration */
448 hr = setup_dinput_options(&newDevice->generic, newDevice->joydev->dev_axes_map);
449 if (hr != DI_OK)
450 goto FAILED1;
451
452 /* Create copy of default data format */
453 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
454 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
455
456 df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
457 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
458
459 for (i = 0; i < newDevice->generic.device_axis_count; i++)
460 {
461 int wine_obj = newDevice->generic.axis_map[i];
462
463 if (wine_obj < 0) continue;
464
465 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
466 if (wine_obj < 8)
467 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
468 else
469 {
470 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
471 i++; /* POV takes 2 axes */
472 }
473 }
474 for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
475 {
476 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
477 df->rgodf[idx ].pguid = &GUID_Button;
478 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
479 }
480 newDevice->generic.base.data_format.wine_df = df;
481
482 /* initialize default properties */
483 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
484 newDevice->generic.props[i].lDevMin = -32767;
485 newDevice->generic.props[i].lDevMax = +32767;
486 newDevice->generic.props[i].lMin = 0;
487 newDevice->generic.props[i].lMax = 0xffff;
488 newDevice->generic.props[i].lDeadZone = newDevice->generic.deadzone; /* % * 1000 */
489 newDevice->generic.props[i].lSaturation = 0;
490 }
491
492 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
493
494 EnterCriticalSection(&dinput->crit);
495 list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
496 LeaveCriticalSection(&dinput->crit);
497
498 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
499 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
500
501 ddi.dwSize = sizeof(ddi);
502 fill_joystick_dideviceinstanceW(&ddi, newDevice->generic.base.dinput->dwVersion, index);
503 newDevice->generic.devcaps.dwDevType = ddi.dwDevType;
504
505 newDevice->generic.devcaps.dwFFSamplePeriod = 0;
506 newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
507 newDevice->generic.devcaps.dwFirmwareRevision = 0;
508 newDevice->generic.devcaps.dwHardwareRevision = 0;
509 newDevice->generic.devcaps.dwFFDriverVersion = 0;
510
511 if (TRACE_ON(dinput)) {
512 _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
513 for (i = 0; i < (newDevice->generic.device_axis_count); i++)
514 TRACE("axis_map[%d] = %d\n", i, newDevice->generic.axis_map[i]);
515 _dump_DIDEVCAPS(&newDevice->generic.devcaps);
516 }
517
518 *pdev = newDevice;
519
520 return DI_OK;
521
522 FAILED:
523 hr = DIERR_OUTOFMEMORY;
524 FAILED1:
525 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
526 HeapFree(GetProcessHeap(), 0, df);
527 release_DataFormat(&newDevice->generic.base.data_format);
528 HeapFree(GetProcessHeap(),0,newDevice->generic.axis_map);
529 HeapFree(GetProcessHeap(),0,newDevice);
530 *pdev = 0;
531
532 return hr;
533 }
534
535 /******************************************************************************
536 * get_joystick_index : Get the joystick index from a given GUID
537 */
538 static unsigned short get_joystick_index(REFGUID guid)
539 {
540 GUID wine_joystick = DInput_Wine_Joystick_GUID;
541 GUID dev_guid = *guid;
542
543 wine_joystick.Data3 = 0;
544 dev_guid.Data3 = 0;
545
546 /* for the standard joystick GUID use index 0 */
547 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
548
549 /* for the wine joystick GUIDs use the index stored in Data3 */
550 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
551
552 return MAX_JOYSTICKS;
553 }
554
555 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
556 {
557 unsigned short index;
558
559 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
560 find_joystick_devices();
561 *pdev = NULL;
562
563 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
564 joystick_devices_count && index < joystick_devices_count)
565 {
566 JoystickImpl *This;
567 HRESULT hr;
568
569 if (riid == NULL)
570 ;/* nothing */
571 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
572 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
573 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
574 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
575 {
576 unicode = 0;
577 }
578 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
579 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
580 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
581 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
582 {
583 unicode = 1;
584 }
585 else
586 {
587 WARN("no interface\n");
588 return DIERR_NOINTERFACE;
589 }
590
591 hr = alloc_device(rguid, dinput, &This, index);
592 if (!This) return hr;
593
594 if (unicode)
595 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
596 else
597 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
598
599 return hr;
600 }
601
602 return DIERR_DEVICENOTREG;
603 }
604
605 #undef MAX_JOYSTICKS
606
607 const struct dinput_device joystick_linux_device = {
608 "Wine Linux joystick driver",
609 joydev_enum_deviceA,
610 joydev_enum_deviceW,
611 joydev_create_device
612 };
613
614 /******************************************************************************
615 * Acquire : gets exclusive control of the joystick
616 */
617 static HRESULT WINAPI JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
618 {
619 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
620 HRESULT res;
621
622 TRACE("(%p)\n",This);
623
624 res = IDirectInputDevice2WImpl_Acquire(iface);
625 if (res != DI_OK)
626 return res;
627
628 /* open the joystick device */
629 if (This->joyfd==-1) {
630 TRACE("opening joystick device %s\n", This->joydev->device);
631
632 This->joyfd = open(This->joydev->device, O_RDONLY);
633 if (This->joyfd==-1) {
634 ERR("open(%s) failed: %s\n", This->joydev->device, strerror(errno));
635 IDirectInputDevice2WImpl_Unacquire(iface);
636 return DIERR_NOTFOUND;
637 }
638 }
639
640 return DI_OK;
641 }
642
643 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
644 {
645 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
646 return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
647 }
648
649 /******************************************************************************
650 * GetProperty : get input device properties
651 */
652 static HRESULT WINAPI JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
653 {
654 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
655
656 TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
657 _dump_DIPROPHEADER(pdiph);
658
659 if (!IS_DIPROP(rguid)) return DI_OK;
660
661 switch (LOWORD(rguid)) {
662
663 case (DWORD_PTR) DIPROP_VIDPID:
664 {
665 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
666
667 if (!This->joydev->product_id || !This->joydev->vendor_id)
668 return DIERR_UNSUPPORTED;
669 pd->dwData = MAKELONG(This->joydev->vendor_id, This->joydev->product_id);
670 TRACE("DIPROP_VIDPID(%08x)\n", pd->dwData);
671 break;
672 }
673 case (DWORD_PTR) DIPROP_JOYSTICKID:
674 {
675 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
676
677 pd->dwData = get_joystick_index(&This->generic.base.guid);
678 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
679 break;
680 }
681
682 default:
683 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
684 }
685
686 return DI_OK;
687 }
688
689 static HRESULT WINAPI JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
690 {
691 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
692 return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
693 }
694
695 /******************************************************************************
696 * GetDeviceInfo : get information about a device's identity
697 */
698 static HRESULT WINAPI JoystickLinuxAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface, LPDIDEVICEINSTANCEA ddi)
699 {
700 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
701
702 TRACE("(%p) %p\n", This, ddi);
703
704 if (ddi == NULL) return E_POINTER;
705 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
706 (ddi->dwSize != sizeof(DIDEVICEINSTANCEA)))
707 return DIERR_INVALIDPARAM;
708
709 fill_joystick_dideviceinstanceA( ddi, This->generic.base.dinput->dwVersion,
710 get_joystick_index(&This->generic.base.guid) );
711 return DI_OK;
712 }
713
714 static HRESULT WINAPI JoystickLinuxWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW ddi)
715 {
716 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
717
718 TRACE("(%p) %p\n", This, ddi);
719
720 if (ddi == NULL) return E_POINTER;
721 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
722 (ddi->dwSize != sizeof(DIDEVICEINSTANCEW)))
723 return DIERR_INVALIDPARAM;
724
725 fill_joystick_dideviceinstanceW( ddi, This->generic.base.dinput->dwVersion,
726 get_joystick_index(&This->generic.base.guid) );
727 return DI_OK;
728 }
729
730 /******************************************************************************
731 * Unacquire : frees the joystick
732 */
733 static HRESULT WINAPI JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
734 {
735 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
736 HRESULT res;
737
738 TRACE("(%p)\n",This);
739
740 res = IDirectInputDevice2WImpl_Unacquire(iface);
741
742 if (res != DI_OK)
743 return res;
744
745 if (This->joyfd!=-1) {
746 TRACE("closing joystick device\n");
747 close(This->joyfd);
748 This->joyfd = -1;
749 return DI_OK;
750 }
751
752 return DI_NOEFFECT;
753 }
754
755 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
756 {
757 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
758 return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
759 }
760
761 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
762 {
763 struct pollfd plfd;
764 struct js_event jse;
765 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
766
767 TRACE("(%p)\n", This);
768
769 if (This->joyfd==-1) {
770 WARN("no device\n");
771 return;
772 }
773 while (1)
774 {
775 LONG value;
776 int inst_id = -1;
777
778 plfd.fd = This->joyfd;
779 plfd.events = POLLIN;
780 if (poll(&plfd,1,0) != 1)
781 return;
782 /* we have one event, so we can read */
783 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
784 return;
785 }
786 TRACE("js_event: type 0x%x, number %d, value %d\n",
787 jse.type,jse.number,jse.value);
788 if (jse.type & JS_EVENT_BUTTON)
789 {
790 if (jse.number >= This->generic.devcaps.dwButtons) return;
791
792 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
793 This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
794 }
795 else if (jse.type & JS_EVENT_AXIS)
796 {
797 int number = This->generic.axis_map[jse.number]; /* wine format object index */
798
799 if (number < 0) return;
800 inst_id = number < 8 ? DIDFT_MAKEINSTANCE(number) | DIDFT_ABSAXIS :
801 DIDFT_MAKEINSTANCE(number - 8) | DIDFT_POV;
802 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
803
804 TRACE("changing axis %d => %d\n", jse.number, number);
805 switch (number)
806 {
807 case 0: This->generic.js.lX = value; break;
808 case 1: This->generic.js.lY = value; break;
809 case 2: This->generic.js.lZ = value; break;
810 case 3: This->generic.js.lRx = value; break;
811 case 4: This->generic.js.lRy = value; break;
812 case 5: This->generic.js.lRz = value; break;
813 case 6: This->generic.js.rglSlider[0] = value; break;
814 case 7: This->generic.js.rglSlider[1] = value; break;
815 case 8: case 9: case 10: case 11:
816 {
817 int idx = number - 8;
818
819 if (jse.number % 2)
820 This->povs[idx].y = jse.value;
821 else
822 This->povs[idx].x = jse.value;
823
824 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
825 break;
826 }
827 default:
828 WARN("axis %d not supported\n", number);
829 }
830 }
831 if (inst_id >= 0)
832 queue_event(iface, inst_id, value, GetCurrentTime(), This->generic.base.dinput->evsequence++);
833 }
834 }
835
836 static const IDirectInputDevice8AVtbl JoystickAvt =
837 {
838 IDirectInputDevice2AImpl_QueryInterface,
839 IDirectInputDevice2AImpl_AddRef,
840 IDirectInputDevice2AImpl_Release,
841 JoystickAGenericImpl_GetCapabilities,
842 IDirectInputDevice2AImpl_EnumObjects,
843 JoystickLinuxAImpl_GetProperty,
844 JoystickAGenericImpl_SetProperty,
845 JoystickLinuxAImpl_Acquire,
846 JoystickLinuxAImpl_Unacquire,
847 JoystickAGenericImpl_GetDeviceState,
848 IDirectInputDevice2AImpl_GetDeviceData,
849 IDirectInputDevice2AImpl_SetDataFormat,
850 IDirectInputDevice2AImpl_SetEventNotification,
851 IDirectInputDevice2AImpl_SetCooperativeLevel,
852 JoystickAGenericImpl_GetObjectInfo,
853 JoystickLinuxAImpl_GetDeviceInfo,
854 IDirectInputDevice2AImpl_RunControlPanel,
855 IDirectInputDevice2AImpl_Initialize,
856 IDirectInputDevice2AImpl_CreateEffect,
857 IDirectInputDevice2AImpl_EnumEffects,
858 IDirectInputDevice2AImpl_GetEffectInfo,
859 IDirectInputDevice2AImpl_GetForceFeedbackState,
860 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
861 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
862 IDirectInputDevice2AImpl_Escape,
863 JoystickAGenericImpl_Poll,
864 IDirectInputDevice2AImpl_SendDeviceData,
865 IDirectInputDevice7AImpl_EnumEffectsInFile,
866 IDirectInputDevice7AImpl_WriteEffectToFile,
867 JoystickAGenericImpl_BuildActionMap,
868 JoystickAGenericImpl_SetActionMap,
869 IDirectInputDevice8AImpl_GetImageInfo
870 };
871
872 static const IDirectInputDevice8WVtbl JoystickWvt =
873 {
874 IDirectInputDevice2WImpl_QueryInterface,
875 IDirectInputDevice2WImpl_AddRef,
876 IDirectInputDevice2WImpl_Release,
877 JoystickWGenericImpl_GetCapabilities,
878 IDirectInputDevice2WImpl_EnumObjects,
879 JoystickLinuxWImpl_GetProperty,
880 JoystickWGenericImpl_SetProperty,
881 JoystickLinuxWImpl_Acquire,
882 JoystickLinuxWImpl_Unacquire,
883 JoystickWGenericImpl_GetDeviceState,
884 IDirectInputDevice2WImpl_GetDeviceData,
885 IDirectInputDevice2WImpl_SetDataFormat,
886 IDirectInputDevice2WImpl_SetEventNotification,
887 IDirectInputDevice2WImpl_SetCooperativeLevel,
888 JoystickWGenericImpl_GetObjectInfo,
889 JoystickLinuxWImpl_GetDeviceInfo,
890 IDirectInputDevice2WImpl_RunControlPanel,
891 IDirectInputDevice2WImpl_Initialize,
892 IDirectInputDevice2WImpl_CreateEffect,
893 IDirectInputDevice2WImpl_EnumEffects,
894 IDirectInputDevice2WImpl_GetEffectInfo,
895 IDirectInputDevice2WImpl_GetForceFeedbackState,
896 IDirectInputDevice2WImpl_SendForceFeedbackCommand,
897 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
898 IDirectInputDevice2WImpl_Escape,
899 JoystickWGenericImpl_Poll,
900 IDirectInputDevice2WImpl_SendDeviceData,
901 IDirectInputDevice7WImpl_EnumEffectsInFile,
902 IDirectInputDevice7WImpl_WriteEffectToFile,
903 JoystickWGenericImpl_BuildActionMap,
904 JoystickWGenericImpl_SetActionMap,
905 IDirectInputDevice8WImpl_GetImageInfo
906 };
907
908 #else /* HAVE_LINUX_22_JOYSTICK_API */
909
910 const struct dinput_device joystick_linux_device = {
911 "Wine Linux joystick driver",
912 NULL,
913 NULL,
914 NULL
915 };
916
917 #endif /* HAVE_LINUX_22_JOYSTICK_API */