mscms/tests: Inline a simple string.
[wine.git] / dlls / dinput / joystick_linux.c
blob2b26eb806d3cd697be4f9e3561bed56cd3028719
1 /* DirectInput Joystick device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
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.
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.
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
23 * To Do:
24 * dead zone
25 * force feedback
28 #include "config.h"
29 #include "wine/port.h"
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
40 #endif
41 #include <fcntl.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
44 #endif
45 #include <errno.h>
46 #ifdef HAVE_LINUX_IOCTL_H
47 # include <linux/ioctl.h>
48 #endif
49 #ifdef HAVE_LINUX_JOYSTICK_H
50 # include <linux/joystick.h>
51 # undef SW_MAX
52 #endif
53 #ifdef HAVE_SYS_POLL_H
54 # include <sys/poll.h>
55 #endif
57 #include "wine/debug.h"
58 #include "wine/unicode.h"
59 #include "windef.h"
60 #include "winbase.h"
61 #include "winerror.h"
62 #include "devguid.h"
63 #include "dinput.h"
65 #include "dinput_private.h"
66 #include "device_private.h"
67 #include "joystick_private.h"
69 #ifdef HAVE_LINUX_22_JOYSTICK_API
71 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
73 #define JOYDEV_NEW "/dev/input/js"
74 #define JOYDEV_OLD "/dev/js"
75 #define JOYDEVDRIVER " (js)"
77 struct JoyDev
79 char device[MAX_PATH];
80 char name[MAX_PATH];
81 GUID guid_product;
83 BYTE axis_count;
84 BYTE button_count;
85 int *dev_axes_map;
87 WORD vendor_id, product_id, bus_type;
89 BOOL is_joystick;
92 typedef struct JoystickImpl JoystickImpl;
93 static const IDirectInputDevice8AVtbl JoystickAvt;
94 static const IDirectInputDevice8WVtbl JoystickWvt;
95 struct JoystickImpl
97 struct JoystickGenericImpl generic;
99 struct JoyDev *joydev;
101 /* joystick private */
102 int joyfd;
103 POINTL povs[4];
106 static inline JoystickImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
108 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface),
109 JoystickGenericImpl, base), JoystickImpl, generic);
111 static inline JoystickImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
113 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface),
114 JoystickGenericImpl, base), JoystickImpl, generic);
117 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickImpl *This)
119 return &This->generic.base.IDirectInputDevice8W_iface;
122 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
123 0x9e573ed9,
124 0x7734,
125 0x11d2,
126 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
129 #define MAX_JOYSTICKS 64
130 static INT joystick_devices_count = -1;
131 static struct JoyDev *joystick_devices;
133 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface);
135 #define SYS_PATH_FORMAT "/sys/class/input/js%d/device/id/%s"
136 static BOOL read_sys_id_variable(int index, const char *property, WORD *value)
138 char sys_path[sizeof(SYS_PATH_FORMAT) + 16], id_str[5];
139 int sys_fd;
140 BOOL ret = FALSE;
142 sprintf(sys_path, SYS_PATH_FORMAT, index, property);
143 if ((sys_fd = open(sys_path, O_RDONLY)) != -1)
145 if (read(sys_fd, id_str, 4) == 4)
147 id_str[4] = '\0';
148 *value = strtol(id_str, NULL, 16);
149 ret = TRUE;
152 close(sys_fd);
154 return ret;
156 #undef SYS_PATH_FORMAT
158 static INT find_joystick_devices(void)
160 INT i;
162 if (joystick_devices_count != -1) return joystick_devices_count;
164 joystick_devices_count = 0;
165 for (i = 0; i < MAX_JOYSTICKS; i++)
167 int fd;
168 struct JoyDev joydev, *new_joydevs;
169 BYTE axes_map[ABS_MAX + 1];
170 SHORT btn_map[KEY_MAX - BTN_MISC + 1];
171 BOOL non_js = FALSE;
173 snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_NEW, i);
174 if ((fd = open(joydev.device, O_RDONLY)) == -1)
176 snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_OLD, i);
177 if ((fd = open(joydev.device, O_RDONLY)) == -1) continue;
180 strcpy(joydev.name, "Wine Joystick");
181 #if defined(JSIOCGNAME)
182 if (ioctl(fd, JSIOCGNAME(sizeof(joydev.name) - sizeof(JOYDEVDRIVER)), joydev.name) < 0)
183 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joydev.device, strerror(errno));
184 #endif
186 /* Append driver name */
187 strcat(joydev.name, JOYDEVDRIVER);
189 if (device_disabled_registry(joydev.name)) {
190 close(fd);
191 continue;
194 #ifdef JSIOCGAXES
195 if (ioctl(fd, JSIOCGAXES, &joydev.axis_count) < 0)
197 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defaulting to 2\n", joydev.device, strerror(errno));
198 joydev.axis_count = 2;
200 #else
201 WARN("reading number of joystick axes unsupported in this platform, defaulting to 2\n");
202 joydev.axis_count = 2;
203 #endif
204 #ifdef JSIOCGBUTTONS
205 if (ioctl(fd, JSIOCGBUTTONS, &joydev.button_count) < 0)
207 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defaulting to 2\n", joydev.device, strerror(errno));
208 joydev.button_count = 2;
210 #else
211 WARN("reading number of joystick buttons unsupported in this platform, defaulting to 2\n");
212 joydev.button_count = 2;
213 #endif
215 joydev.is_joystick = FALSE;
216 if (ioctl(fd, JSIOCGBTNMAP, btn_map) < 0)
218 WARN("ioctl(%s,JSIOCGBTNMAP) failed: %s\n", joydev.device, strerror(errno));
220 else
222 INT j;
223 /* in lieu of properly reporting HID usage, detect presence of
224 * "joystick buttons" and report those devices as joysticks instead of
225 * gamepads */
226 for (j = 0; !joydev.is_joystick && j < joydev.button_count; j++)
228 switch (btn_map[j])
230 case BTN_TRIGGER:
231 case BTN_THUMB:
232 case BTN_THUMB2:
233 case BTN_TOP:
234 case BTN_TOP2:
235 case BTN_PINKIE:
236 case BTN_BASE:
237 case BTN_BASE2:
238 case BTN_BASE3:
239 case BTN_BASE4:
240 case BTN_BASE5:
241 case BTN_BASE6:
242 case BTN_DEAD:
243 joydev.is_joystick = TRUE;
244 break;
245 case BTN_MOUSE:
246 case BTN_STYLUS:
247 non_js = TRUE;
248 break;
249 default:
250 break;
255 if(non_js)
257 TRACE("Non-joystick detected. Skipping\n");
258 close(fd);
259 continue;
262 if (ioctl(fd, JSIOCGAXMAP, axes_map) < 0)
264 WARN("ioctl(%s,JSIOCGAXMAP) failed: %s\n", joydev.device, strerror(errno));
265 joydev.dev_axes_map = NULL;
267 else
268 if ((joydev.dev_axes_map = HeapAlloc(GetProcessHeap(), 0, joydev.axis_count * sizeof(int))))
270 INT j, found_axes = 0;
272 /* Remap to DI numbers */
273 for (j = 0; j < joydev.axis_count; j++)
275 if (axes_map[j] < 8)
277 /* Axis match 1-to-1 */
278 joydev.dev_axes_map[j] = j;
279 found_axes++;
281 else if (axes_map[j] <= 10)
283 /* Axes 8 through 10 are Wheel, Gas and Brake,
284 * remap to 0, 1 and 2
286 joydev.dev_axes_map[j] = axes_map[j] - 8;
287 found_axes++;
289 else if (axes_map[j] == 16 ||
290 axes_map[j] == 17)
292 /* POV axis */
293 joydev.dev_axes_map[j] = 8;
294 found_axes++;
296 else
297 joydev.dev_axes_map[j] = -1;
300 /* If no axes were configured but there are axes assume a 1-to-1 (wii controller) */
301 if (joydev.axis_count && !found_axes)
303 int axes_limit = min(joydev.axis_count, 8); /* generic driver limit */
305 ERR("Incoherent joystick data, advertised %d axes, detected 0. Assuming 1-to-1.\n",
306 joydev.axis_count);
307 for (j = 0; j < axes_limit; j++)
308 joydev.dev_axes_map[j] = j;
310 joydev.axis_count = axes_limit;
314 /* Find vendor_id and product_id in sysfs */
315 joydev.vendor_id = 0;
316 joydev.product_id = 0;
318 read_sys_id_variable(i, "vendor", &joydev.vendor_id);
319 read_sys_id_variable(i, "product", &joydev.product_id);
320 read_sys_id_variable(i, "bustype", &joydev.bus_type);
322 if (joydev.vendor_id == 0 || joydev.product_id == 0)
324 joydev.guid_product = DInput_Wine_Joystick_GUID;
326 else
328 /* Concatenate product_id with vendor_id to mimic Windows behaviour */
329 joydev.guid_product = DInput_PIDVID_Product_GUID;
330 joydev.guid_product.Data1 = MAKELONG(joydev.vendor_id, joydev.product_id);
333 close(fd);
335 if (!joystick_devices_count)
336 new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
337 else
338 new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joystick_devices,
339 (joystick_devices_count + 1) * sizeof(struct JoyDev));
340 if (!new_joydevs) continue;
342 TRACE("Found a joystick on %s: %s\n with %d axes and %d buttons\n", joydev.device,
343 joydev.name, joydev.axis_count, joydev.button_count);
345 joystick_devices = new_joydevs;
346 joystick_devices[joystick_devices_count++] = joydev;
349 return joystick_devices_count;
352 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
354 DWORD dwSize = lpddi->dwSize;
356 TRACE("%d %p\n", dwSize, lpddi);
357 memset(lpddi, 0, dwSize);
359 /* Return joystick */
360 lpddi->dwSize = dwSize;
361 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
362 lpddi->guidInstance.Data3 = id;
363 lpddi->guidProduct = joystick_devices[id].guid_product;
364 lpddi->dwDevType = get_device_type(version, joystick_devices[id].is_joystick);
366 /* Assume the joystick as HID if it is attached to USB bus and has a valid VID/PID */
367 if (joystick_devices[id].bus_type == BUS_USB &&
368 joystick_devices[id].vendor_id && joystick_devices[id].product_id)
370 lpddi->dwDevType |= DIDEVTYPE_HID;
371 lpddi->wUsagePage = 0x01; /* Desktop */
372 if (joystick_devices[id].is_joystick)
373 lpddi->wUsage = 0x04; /* Joystick */
374 else
375 lpddi->wUsage = 0x05; /* Game Pad */
378 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
379 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszProductName, MAX_PATH);
380 lpddi->guidFFDriver = GUID_NULL;
383 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
385 DIDEVICEINSTANCEW lpddiW;
386 DWORD dwSize = lpddi->dwSize;
388 lpddiW.dwSize = sizeof(lpddiW);
389 fill_joystick_dideviceinstanceW(&lpddiW, version, id);
391 TRACE("%d %p\n", dwSize, lpddi);
392 memset(lpddi, 0, dwSize);
394 /* Convert W->A */
395 lpddi->dwSize = dwSize;
396 lpddi->guidInstance = lpddiW.guidInstance;
397 lpddi->guidProduct = lpddiW.guidProduct;
398 lpddi->dwDevType = lpddiW.dwDevType;
399 strcpy(lpddi->tszInstanceName, joystick_devices[id].name);
400 strcpy(lpddi->tszProductName, joystick_devices[id].name);
401 lpddi->guidFFDriver = lpddiW.guidFFDriver;
402 lpddi->wUsagePage = lpddiW.wUsagePage;
403 lpddi->wUsage = lpddiW.wUsage;
406 static HRESULT joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
408 int fd = -1;
410 if (id >= find_joystick_devices()) return E_FAIL;
412 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
413 WARN("force feedback not supported\n");
414 return S_FALSE;
417 if ((dwDevType == 0) ||
418 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version >= 0x0300 && version < 0x0800)) ||
419 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
420 /* check whether we have a joystick */
421 if ((fd = open(joystick_devices[id].device, O_RDONLY)) == -1)
423 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
424 return S_FALSE;
426 fill_joystick_dideviceinstanceA( lpddi, version, id );
427 close(fd);
428 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
429 return S_OK;
432 return S_FALSE;
435 static HRESULT joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
437 int fd = -1;
439 if (id >= find_joystick_devices()) return E_FAIL;
441 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
442 WARN("force feedback not supported\n");
443 return S_FALSE;
446 if ((dwDevType == 0) ||
447 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version >= 0x0300 && version < 0x0800)) ||
448 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
449 /* check whether we have a joystick */
450 if ((fd = open(joystick_devices[id].device, O_RDONLY)) == -1)
452 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
453 return S_FALSE;
455 fill_joystick_dideviceinstanceW( lpddi, version, id );
456 close(fd);
457 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
458 return S_OK;
461 return S_FALSE;
464 static HRESULT alloc_device(REFGUID rguid, IDirectInputImpl *dinput,
465 JoystickImpl **pdev, unsigned short index)
467 DWORD i;
468 JoystickImpl* newDevice;
469 HRESULT hr;
470 LPDIDATAFORMAT df = NULL;
471 int idx = 0;
472 DIDEVICEINSTANCEW ddi;
474 TRACE("%s %p %p %hu\n", debugstr_guid(rguid), dinput, pdev, index);
476 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
477 if (newDevice == 0) {
478 WARN("out of memory\n");
479 *pdev = 0;
480 return DIERR_OUTOFMEMORY;
483 newDevice->joydev = &joystick_devices[index];
484 newDevice->joyfd = -1;
485 newDevice->generic.guidInstance = DInput_Wine_Joystick_GUID;
486 newDevice->generic.guidInstance.Data3 = index;
487 newDevice->generic.guidProduct = DInput_Wine_Joystick_GUID;
488 newDevice->generic.joy_polldev = joy_polldev;
489 newDevice->generic.name = newDevice->joydev->name;
490 newDevice->generic.device_axis_count = newDevice->joydev->axis_count;
491 newDevice->generic.devcaps.dwButtons = newDevice->joydev->button_count;
493 if (newDevice->generic.devcaps.dwButtons > 128)
495 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
496 newDevice->generic.devcaps.dwButtons = 128;
499 newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
500 newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
501 newDevice->generic.base.ref = 1;
502 newDevice->generic.base.dinput = dinput;
503 newDevice->generic.base.guid = *rguid;
504 InitializeCriticalSection(&newDevice->generic.base.crit);
505 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
507 /* setup_dinput_options may change these */
508 newDevice->generic.deadzone = 0;
510 /* do any user specified configuration */
511 hr = setup_dinput_options(&newDevice->generic, newDevice->joydev->dev_axes_map);
512 if (hr != DI_OK)
513 goto FAILED1;
515 /* Create copy of default data format */
516 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
517 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
519 df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
520 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
522 for (i = 0; i < newDevice->generic.device_axis_count; i++)
524 int wine_obj = newDevice->generic.axis_map[i];
526 if (wine_obj < 0) continue;
528 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
529 if (wine_obj < 8)
530 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
531 else
533 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
534 i++; /* POV takes 2 axes */
537 for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
539 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
540 df->rgodf[idx ].pguid = &GUID_Button;
541 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
543 newDevice->generic.base.data_format.wine_df = df;
545 /* initialize default properties */
546 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
547 newDevice->generic.props[i].lDevMin = -32767;
548 newDevice->generic.props[i].lDevMax = +32767;
549 newDevice->generic.props[i].lMin = 0;
550 newDevice->generic.props[i].lMax = 0xffff;
551 newDevice->generic.props[i].lDeadZone = newDevice->generic.deadzone; /* % * 1000 */
552 newDevice->generic.props[i].lSaturation = 0;
555 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
557 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
558 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
560 ddi.dwSize = sizeof(ddi);
561 fill_joystick_dideviceinstanceW(&ddi, newDevice->generic.base.dinput->dwVersion, index);
562 newDevice->generic.devcaps.dwDevType = ddi.dwDevType;
564 newDevice->generic.devcaps.dwFFSamplePeriod = 0;
565 newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
566 newDevice->generic.devcaps.dwFirmwareRevision = 0;
567 newDevice->generic.devcaps.dwHardwareRevision = 0;
568 newDevice->generic.devcaps.dwFFDriverVersion = 0;
570 if (TRACE_ON(dinput)) {
571 _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
572 for (i = 0; i < (newDevice->generic.device_axis_count); i++)
573 TRACE("axis_map[%d] = %d\n", i, newDevice->generic.axis_map[i]);
574 _dump_DIDEVCAPS(&newDevice->generic.devcaps);
577 *pdev = newDevice;
579 return DI_OK;
581 FAILED:
582 hr = DIERR_OUTOFMEMORY;
583 FAILED1:
584 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
585 HeapFree(GetProcessHeap(), 0, df);
586 release_DataFormat(&newDevice->generic.base.data_format);
587 HeapFree(GetProcessHeap(),0,newDevice->generic.axis_map);
588 HeapFree(GetProcessHeap(),0,newDevice);
589 *pdev = 0;
591 return hr;
594 /******************************************************************************
595 * get_joystick_index : Get the joystick index from a given GUID
597 static unsigned short get_joystick_index(REFGUID guid)
599 GUID wine_joystick = DInput_Wine_Joystick_GUID;
600 GUID dev_guid = *guid;
602 wine_joystick.Data3 = 0;
603 dev_guid.Data3 = 0;
605 /* for the standard joystick GUID use index 0 */
606 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
608 /* for the wine joystick GUIDs use the index stored in Data3 */
609 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
611 return MAX_JOYSTICKS;
614 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
616 unsigned short index;
618 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
619 find_joystick_devices();
620 *pdev = NULL;
622 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
623 joystick_devices_count && index < joystick_devices_count)
625 JoystickImpl *This;
626 HRESULT hr;
628 if (riid == NULL)
629 ;/* nothing */
630 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
631 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
632 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
633 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
635 unicode = 0;
637 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
638 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
639 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
640 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
642 unicode = 1;
644 else
646 WARN("no interface\n");
647 return DIERR_NOINTERFACE;
650 hr = alloc_device(rguid, dinput, &This, index);
651 if (!This) return hr;
653 if (unicode)
654 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
655 else
656 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
658 return hr;
661 return DIERR_DEVICENOTREG;
664 #undef MAX_JOYSTICKS
666 const struct dinput_device joystick_linux_device = {
667 "Wine Linux joystick driver",
668 joydev_enum_deviceA,
669 joydev_enum_deviceW,
670 joydev_create_device
673 /******************************************************************************
674 * Acquire : gets exclusive control of the joystick
676 static HRESULT WINAPI JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
678 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
679 HRESULT res;
681 TRACE("(%p)\n",This);
683 res = IDirectInputDevice2WImpl_Acquire(iface);
684 if (res != DI_OK)
685 return res;
687 /* open the joystick device */
688 if (This->joyfd==-1) {
689 TRACE("opening joystick device %s\n", This->joydev->device);
691 This->joyfd = open(This->joydev->device, O_RDONLY);
692 if (This->joyfd==-1) {
693 ERR("open(%s) failed: %s\n", This->joydev->device, strerror(errno));
694 IDirectInputDevice2WImpl_Unacquire(iface);
695 return DIERR_NOTFOUND;
699 return DI_OK;
702 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
704 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
705 return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
708 /******************************************************************************
709 * GetProperty : get input device properties
711 static HRESULT WINAPI JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
713 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
715 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(rguid), pdiph);
716 _dump_DIPROPHEADER(pdiph);
718 if (!IS_DIPROP(rguid)) return DI_OK;
720 switch (LOWORD(rguid)) {
722 case (DWORD_PTR) DIPROP_VIDPID:
724 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
726 if (!This->joydev->product_id || !This->joydev->vendor_id)
727 return DIERR_UNSUPPORTED;
728 pd->dwData = MAKELONG(This->joydev->vendor_id, This->joydev->product_id);
729 TRACE("DIPROP_VIDPID(%08x)\n", pd->dwData);
730 break;
732 case (DWORD_PTR) DIPROP_JOYSTICKID:
734 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
736 pd->dwData = get_joystick_index(&This->generic.base.guid);
737 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
738 break;
741 case (DWORD_PTR) DIPROP_GUIDANDPATH:
743 static const WCHAR formatW[] = {'\\','\\','?','\\','h','i','d','#','v','i','d','_','%','0','4','x','&',
744 'p','i','d','_','%','0','4','x','&','%','s','_','%','h','u',0};
745 static const WCHAR miW[] = {'m','i',0};
746 static const WCHAR igW[] = {'i','g',0};
748 BOOL is_gamepad;
749 LPDIPROPGUIDANDPATH pd = (LPDIPROPGUIDANDPATH)pdiph;
750 WORD vid = This->joydev->vendor_id;
751 WORD pid = This->joydev->product_id;
753 if (!pid || !vid)
754 return DIERR_UNSUPPORTED;
756 is_gamepad = is_xinput_device(&This->generic.devcaps, vid, pid);
757 pd->guidClass = GUID_DEVCLASS_HIDCLASS;
758 sprintfW(pd->wszPath, formatW, vid, pid, is_gamepad ? igW : miW, get_joystick_index(&This->generic.base.guid));
760 TRACE("DIPROP_GUIDANDPATH(%s, %s): returning fake path\n", debugstr_guid(&pd->guidClass), debugstr_w(pd->wszPath));
761 break;
764 default:
765 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
768 return DI_OK;
771 static HRESULT WINAPI JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
773 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
774 return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
777 /******************************************************************************
778 * GetDeviceInfo : get information about a device's identity
780 static HRESULT WINAPI JoystickLinuxAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface, LPDIDEVICEINSTANCEA ddi)
782 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
784 TRACE("(%p) %p\n", This, ddi);
786 if (ddi == NULL) return E_POINTER;
787 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
788 (ddi->dwSize != sizeof(DIDEVICEINSTANCEA)))
789 return DIERR_INVALIDPARAM;
791 fill_joystick_dideviceinstanceA( ddi, This->generic.base.dinput->dwVersion,
792 get_joystick_index(&This->generic.base.guid) );
793 return DI_OK;
796 static HRESULT WINAPI JoystickLinuxWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW ddi)
798 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
800 TRACE("(%p) %p\n", This, ddi);
802 if (ddi == NULL) return E_POINTER;
803 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
804 (ddi->dwSize != sizeof(DIDEVICEINSTANCEW)))
805 return DIERR_INVALIDPARAM;
807 fill_joystick_dideviceinstanceW( ddi, This->generic.base.dinput->dwVersion,
808 get_joystick_index(&This->generic.base.guid) );
809 return DI_OK;
812 /******************************************************************************
813 * Unacquire : frees the joystick
815 static HRESULT WINAPI JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
817 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
818 HRESULT res;
820 TRACE("(%p)\n",This);
822 res = IDirectInputDevice2WImpl_Unacquire(iface);
824 if (res != DI_OK)
825 return res;
827 if (This->joyfd!=-1) {
828 TRACE("closing joystick device\n");
829 close(This->joyfd);
830 This->joyfd = -1;
831 return DI_OK;
834 return DI_NOEFFECT;
837 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
839 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
840 return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
843 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
845 struct pollfd plfd;
846 struct js_event jse;
847 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
849 TRACE("(%p)\n", This);
851 if (This->joyfd==-1) {
852 WARN("no device\n");
853 return;
855 while (1)
857 LONG value;
858 int inst_id = -1;
860 plfd.fd = This->joyfd;
861 plfd.events = POLLIN;
862 if (poll(&plfd,1,0) != 1)
863 return;
864 /* we have one event, so we can read */
865 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
866 return;
868 TRACE("js_event: type 0x%x, number %d, value %d\n",
869 jse.type,jse.number,jse.value);
870 if (jse.type & JS_EVENT_BUTTON)
872 if (jse.number >= This->generic.devcaps.dwButtons) return;
874 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
875 This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
877 else if (jse.type & JS_EVENT_AXIS)
879 int number = This->generic.axis_map[jse.number]; /* wine format object index */
881 if (number < 0) return;
882 inst_id = number < 8 ? DIDFT_MAKEINSTANCE(number) | DIDFT_ABSAXIS :
883 DIDFT_MAKEINSTANCE(number - 8) | DIDFT_POV;
884 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
886 TRACE("changing axis %d => %d\n", jse.number, number);
887 switch (number)
889 case 0: This->generic.js.lX = value; break;
890 case 1: This->generic.js.lY = value; break;
891 case 2: This->generic.js.lZ = value; break;
892 case 3: This->generic.js.lRx = value; break;
893 case 4: This->generic.js.lRy = value; break;
894 case 5: This->generic.js.lRz = value; break;
895 case 6: This->generic.js.rglSlider[0] = value; break;
896 case 7: This->generic.js.rglSlider[1] = value; break;
897 case 8: case 9: case 10: case 11:
899 int idx = number - 8;
901 if (jse.number % 2)
902 This->povs[idx].y = jse.value;
903 else
904 This->povs[idx].x = jse.value;
906 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
907 break;
909 default:
910 WARN("axis %d not supported\n", number);
913 if (inst_id >= 0)
914 queue_event(iface, inst_id, value, GetCurrentTime(), This->generic.base.dinput->evsequence++);
918 static const IDirectInputDevice8AVtbl JoystickAvt =
920 IDirectInputDevice2AImpl_QueryInterface,
921 IDirectInputDevice2AImpl_AddRef,
922 IDirectInputDevice2AImpl_Release,
923 JoystickAGenericImpl_GetCapabilities,
924 IDirectInputDevice2AImpl_EnumObjects,
925 JoystickLinuxAImpl_GetProperty,
926 JoystickAGenericImpl_SetProperty,
927 JoystickLinuxAImpl_Acquire,
928 JoystickLinuxAImpl_Unacquire,
929 JoystickAGenericImpl_GetDeviceState,
930 IDirectInputDevice2AImpl_GetDeviceData,
931 IDirectInputDevice2AImpl_SetDataFormat,
932 IDirectInputDevice2AImpl_SetEventNotification,
933 IDirectInputDevice2AImpl_SetCooperativeLevel,
934 JoystickAGenericImpl_GetObjectInfo,
935 JoystickLinuxAImpl_GetDeviceInfo,
936 IDirectInputDevice2AImpl_RunControlPanel,
937 IDirectInputDevice2AImpl_Initialize,
938 IDirectInputDevice2AImpl_CreateEffect,
939 IDirectInputDevice2AImpl_EnumEffects,
940 IDirectInputDevice2AImpl_GetEffectInfo,
941 IDirectInputDevice2AImpl_GetForceFeedbackState,
942 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
943 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
944 IDirectInputDevice2AImpl_Escape,
945 JoystickAGenericImpl_Poll,
946 IDirectInputDevice2AImpl_SendDeviceData,
947 IDirectInputDevice7AImpl_EnumEffectsInFile,
948 IDirectInputDevice7AImpl_WriteEffectToFile,
949 JoystickAGenericImpl_BuildActionMap,
950 JoystickAGenericImpl_SetActionMap,
951 IDirectInputDevice8AImpl_GetImageInfo
954 static const IDirectInputDevice8WVtbl JoystickWvt =
956 IDirectInputDevice2WImpl_QueryInterface,
957 IDirectInputDevice2WImpl_AddRef,
958 IDirectInputDevice2WImpl_Release,
959 JoystickWGenericImpl_GetCapabilities,
960 IDirectInputDevice2WImpl_EnumObjects,
961 JoystickLinuxWImpl_GetProperty,
962 JoystickWGenericImpl_SetProperty,
963 JoystickLinuxWImpl_Acquire,
964 JoystickLinuxWImpl_Unacquire,
965 JoystickWGenericImpl_GetDeviceState,
966 IDirectInputDevice2WImpl_GetDeviceData,
967 IDirectInputDevice2WImpl_SetDataFormat,
968 IDirectInputDevice2WImpl_SetEventNotification,
969 IDirectInputDevice2WImpl_SetCooperativeLevel,
970 JoystickWGenericImpl_GetObjectInfo,
971 JoystickLinuxWImpl_GetDeviceInfo,
972 IDirectInputDevice2WImpl_RunControlPanel,
973 IDirectInputDevice2WImpl_Initialize,
974 IDirectInputDevice2WImpl_CreateEffect,
975 IDirectInputDevice2WImpl_EnumEffects,
976 IDirectInputDevice2WImpl_GetEffectInfo,
977 IDirectInputDevice2WImpl_GetForceFeedbackState,
978 IDirectInputDevice2WImpl_SendForceFeedbackCommand,
979 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
980 IDirectInputDevice2WImpl_Escape,
981 JoystickWGenericImpl_Poll,
982 IDirectInputDevice2WImpl_SendDeviceData,
983 IDirectInputDevice7WImpl_EnumEffectsInFile,
984 IDirectInputDevice7WImpl_WriteEffectToFile,
985 JoystickWGenericImpl_BuildActionMap,
986 JoystickWGenericImpl_SetActionMap,
987 IDirectInputDevice8WImpl_GetImageInfo
990 #else /* HAVE_LINUX_22_JOYSTICK_API */
992 const struct dinput_device joystick_linux_device = {
993 "Wine Linux joystick driver",
994 NULL,
995 NULL,
996 NULL
999 #endif /* HAVE_LINUX_22_JOYSTICK_API */