vbscript/tests: Fix first-day-of-week test on mixed locales.
[wine.git] / dlls / dinput / joystick_linux.c
blob963e62f017c0b08d5a522719d3c963f8dee240fd
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 "dinput.h"
64 #include "dinput_private.h"
65 #include "device_private.h"
66 #include "joystick_private.h"
68 #ifdef HAVE_LINUX_22_JOYSTICK_API
70 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
72 #define JOYDEV_NEW "/dev/input/js"
73 #define JOYDEV_OLD "/dev/js"
74 #define JOYDEVDRIVER " (js)"
76 struct JoyDev
78 char device[MAX_PATH];
79 char name[MAX_PATH];
80 GUID guid_product;
82 BYTE axis_count;
83 BYTE button_count;
84 int *dev_axes_map;
86 WORD vendor_id, product_id, bus_type;
89 typedef struct JoystickImpl JoystickImpl;
90 static const IDirectInputDevice8AVtbl JoystickAvt;
91 static const IDirectInputDevice8WVtbl JoystickWvt;
92 struct JoystickImpl
94 struct JoystickGenericImpl generic;
96 struct JoyDev *joydev;
98 /* joystick private */
99 int joyfd;
100 POINTL povs[4];
103 static inline JoystickImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
105 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface),
106 JoystickGenericImpl, base), JoystickImpl, generic);
108 static inline JoystickImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
110 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface),
111 JoystickGenericImpl, base), JoystickImpl, generic);
114 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickImpl *This)
116 return &This->generic.base.IDirectInputDevice8W_iface;
119 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
120 0x9e573ed9,
121 0x7734,
122 0x11d2,
123 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
127 * Construct the GUID in the same way of Windows doing this.
128 * Data1 is concatenation of productid and vendorid.
129 * Data2 and Data3 are NULL.
130 * Data4 seems to be a constant.
132 static const GUID DInput_Wine_Joystick_Constant_Part_GUID = {
133 0x000000000,
134 0x0000,
135 0x0000,
136 {0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44}
139 #define MAX_JOYSTICKS 64
140 static INT joystick_devices_count = -1;
141 static struct JoyDev *joystick_devices;
143 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface);
145 #define SYS_PATH_FORMAT "/sys/class/input/js%d/device/id/%s"
146 static BOOL read_sys_id_variable(int index, const char *property, WORD *value)
148 char sys_path[sizeof(SYS_PATH_FORMAT) + 16], id_str[5];
149 int sys_fd;
150 BOOL ret = FALSE;
152 sprintf(sys_path, SYS_PATH_FORMAT, index, property);
153 if ((sys_fd = open(sys_path, O_RDONLY)) != -1)
155 if (read(sys_fd, id_str, 4) == 4)
157 id_str[4] = '\0';
158 *value = strtol(id_str, NULL, 16);
159 ret = TRUE;
162 close(sys_fd);
164 return ret;
166 #undef SYS_PATH_FORMAT
168 static INT find_joystick_devices(void)
170 INT i;
172 if (joystick_devices_count != -1) return joystick_devices_count;
174 joystick_devices_count = 0;
175 for (i = 0; i < MAX_JOYSTICKS; i++)
177 int fd;
178 struct JoyDev joydev, *new_joydevs;
179 BYTE axes_map[ABS_MAX + 1];
181 snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_NEW, i);
182 if ((fd = open(joydev.device, O_RDONLY)) == -1)
184 snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_OLD, i);
185 if ((fd = open(joydev.device, O_RDONLY)) == -1) continue;
188 strcpy(joydev.name, "Wine Joystick");
189 #if defined(JSIOCGNAME)
190 if (ioctl(fd, JSIOCGNAME(sizeof(joydev.name) - sizeof(JOYDEVDRIVER)), joydev.name) < 0)
191 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joydev.device, strerror(errno));
192 #endif
194 /* Append driver name */
195 strcat(joydev.name, JOYDEVDRIVER);
197 if (device_disabled_registry(joydev.name)) {
198 close(fd);
199 continue;
202 #ifdef JSIOCGAXES
203 if (ioctl(fd, JSIOCGAXES, &joydev.axis_count) < 0)
205 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defaulting to 2\n", joydev.device, strerror(errno));
206 joydev.axis_count = 2;
208 #else
209 WARN("reading number of joystick axes unsupported in this platform, defaulting to 2\n");
210 joydev.axis_count = 2;
211 #endif
212 #ifdef JSIOCGBUTTONS
213 if (ioctl(fd, JSIOCGBUTTONS, &joydev.button_count) < 0)
215 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defaulting to 2\n", joydev.device, strerror(errno));
216 joydev.button_count = 2;
218 #else
219 WARN("reading number of joystick buttons unsupported in this platform, defaulting to 2\n");
220 joydev.button_count = 2;
221 #endif
223 if (ioctl(fd, JSIOCGAXMAP, axes_map) < 0)
225 WARN("ioctl(%s,JSIOCGAXMAP) failed: %s\n", joydev.device, strerror(errno));
226 joydev.dev_axes_map = NULL;
228 else
229 if ((joydev.dev_axes_map = HeapAlloc(GetProcessHeap(), 0, joydev.axis_count * sizeof(int))))
231 INT j, found_axes = 0;
233 /* Remap to DI numbers */
234 for (j = 0; j < joydev.axis_count; j++)
236 if (axes_map[j] < 8)
238 /* Axis match 1-to-1 */
239 joydev.dev_axes_map[j] = j;
240 found_axes++;
242 else if (axes_map[j] == 16 ||
243 axes_map[j] == 17)
245 /* POV axis */
246 joydev.dev_axes_map[j] = 8;
247 found_axes++;
249 else
250 joydev.dev_axes_map[j] = -1;
253 /* If no axes were configured but there are axes assume a 1-to-1 (wii controller) */
254 if (joydev.axis_count && !found_axes)
256 int axes_limit = min(joydev.axis_count, 8); /* generic driver limit */
258 ERR("Incoherent joystick data, advertised %d axes, detected 0. Assuming 1-to-1.\n",
259 joydev.axis_count);
260 for (j = 0; j < axes_limit; j++)
261 joydev.dev_axes_map[j] = j;
263 joydev.axis_count = axes_limit;
267 /* Find vendor_id and product_id in sysfs */
268 joydev.vendor_id = 0;
269 joydev.product_id = 0;
271 read_sys_id_variable(i, "vendor", &joydev.vendor_id);
272 read_sys_id_variable(i, "product", &joydev.product_id);
273 read_sys_id_variable(i, "bustype", &joydev.bus_type);
275 if (joydev.vendor_id == 0 || joydev.product_id == 0)
277 joydev.guid_product = DInput_Wine_Joystick_GUID;
279 else
281 /* Concatenate product_id with vendor_id to mimic Windows behaviour */
282 joydev.guid_product = DInput_Wine_Joystick_Constant_Part_GUID;
283 joydev.guid_product.Data1 = MAKELONG(joydev.vendor_id, joydev.product_id);
286 close(fd);
288 if (!joystick_devices_count)
289 new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
290 else
291 new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joystick_devices,
292 (joystick_devices_count + 1) * sizeof(struct JoyDev));
293 if (!new_joydevs) continue;
295 TRACE("Found a joystick on %s: %s\n with %d axes and %d buttons\n", joydev.device,
296 joydev.name, joydev.axis_count, joydev.button_count);
298 joystick_devices = new_joydevs;
299 joystick_devices[joystick_devices_count++] = joydev;
302 return joystick_devices_count;
305 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
307 DWORD dwSize = lpddi->dwSize;
309 TRACE("%d %p\n", dwSize, lpddi);
310 memset(lpddi, 0, dwSize);
312 /* Return joystick */
313 lpddi->dwSize = dwSize;
314 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
315 lpddi->guidInstance.Data3 = id;
316 lpddi->guidProduct = joystick_devices[id].guid_product;
317 /* we only support traditional joysticks for now */
318 if (version >= 0x0800)
319 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
320 else
321 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
323 /* Assume the joystick as HID if it is attached to USB bus and has a valid VID/PID */
324 if (joystick_devices[id].bus_type == BUS_USB &&
325 joystick_devices[id].vendor_id && joystick_devices[id].product_id)
327 lpddi->dwDevType |= DIDEVTYPE_HID;
328 lpddi->wUsagePage = 0x01; /* Desktop */
329 if (lpddi->dwDevType == DI8DEVTYPE_JOYSTICK || lpddi->dwDevType == DIDEVTYPE_JOYSTICK)
330 lpddi->wUsage = 0x04; /* Joystick */
331 else
332 lpddi->wUsage = 0x05; /* Game Pad */
335 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
336 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszProductName, MAX_PATH);
337 lpddi->guidFFDriver = GUID_NULL;
340 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
342 DIDEVICEINSTANCEW lpddiW;
343 DWORD dwSize = lpddi->dwSize;
345 lpddiW.dwSize = sizeof(lpddiW);
346 fill_joystick_dideviceinstanceW(&lpddiW, version, id);
348 TRACE("%d %p\n", dwSize, lpddi);
349 memset(lpddi, 0, dwSize);
351 /* Convert W->A */
352 lpddi->dwSize = dwSize;
353 lpddi->guidInstance = lpddiW.guidInstance;
354 lpddi->guidProduct = lpddiW.guidProduct;
355 lpddi->dwDevType = lpddiW.dwDevType;
356 strcpy(lpddi->tszInstanceName, joystick_devices[id].name);
357 strcpy(lpddi->tszProductName, joystick_devices[id].name);
358 lpddi->guidFFDriver = lpddiW.guidFFDriver;
359 lpddi->wUsagePage = lpddiW.wUsagePage;
360 lpddi->wUsage = lpddiW.wUsage;
363 static HRESULT joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
365 int fd = -1;
367 if (id >= find_joystick_devices()) return E_FAIL;
369 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
370 WARN("force feedback not supported\n");
371 return S_FALSE;
374 if ((dwDevType == 0) ||
375 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
376 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
377 /* check whether we have a joystick */
378 if ((fd = open(joystick_devices[id].device, O_RDONLY)) == -1)
380 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
381 return S_FALSE;
383 fill_joystick_dideviceinstanceA( lpddi, version, id );
384 close(fd);
385 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
386 return S_OK;
389 return S_FALSE;
392 static HRESULT joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
394 int fd = -1;
396 if (id >= find_joystick_devices()) return E_FAIL;
398 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
399 WARN("force feedback not supported\n");
400 return S_FALSE;
403 if ((dwDevType == 0) ||
404 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
405 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
406 /* check whether we have a joystick */
407 if ((fd = open(joystick_devices[id].device, O_RDONLY)) == -1)
409 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
410 return S_FALSE;
412 fill_joystick_dideviceinstanceW( lpddi, version, id );
413 close(fd);
414 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
415 return S_OK;
418 return S_FALSE;
421 static HRESULT alloc_device(REFGUID rguid, IDirectInputImpl *dinput,
422 JoystickImpl **pdev, unsigned short index)
424 DWORD i;
425 JoystickImpl* newDevice;
426 HRESULT hr;
427 LPDIDATAFORMAT df = NULL;
428 int idx = 0;
429 DIDEVICEINSTANCEW ddi;
431 TRACE("%s %p %p %hu\n", debugstr_guid(rguid), dinput, pdev, index);
433 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
434 if (newDevice == 0) {
435 WARN("out of memory\n");
436 *pdev = 0;
437 return DIERR_OUTOFMEMORY;
440 newDevice->joydev = &joystick_devices[index];
441 newDevice->joyfd = -1;
442 newDevice->generic.guidInstance = DInput_Wine_Joystick_GUID;
443 newDevice->generic.guidInstance.Data3 = index;
444 newDevice->generic.guidProduct = DInput_Wine_Joystick_GUID;
445 newDevice->generic.joy_polldev = joy_polldev;
446 newDevice->generic.name = newDevice->joydev->name;
447 newDevice->generic.device_axis_count = newDevice->joydev->axis_count;
448 newDevice->generic.devcaps.dwButtons = newDevice->joydev->button_count;
450 if (newDevice->generic.devcaps.dwButtons > 128)
452 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
453 newDevice->generic.devcaps.dwButtons = 128;
456 newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
457 newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
458 newDevice->generic.base.ref = 1;
459 newDevice->generic.base.dinput = dinput;
460 newDevice->generic.base.guid = *rguid;
461 InitializeCriticalSection(&newDevice->generic.base.crit);
462 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
464 /* setup_dinput_options may change these */
465 newDevice->generic.deadzone = 0;
467 /* do any user specified configuration */
468 hr = setup_dinput_options(&newDevice->generic, newDevice->joydev->dev_axes_map);
469 if (hr != DI_OK)
470 goto FAILED1;
472 /* Create copy of default data format */
473 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
474 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
476 df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
477 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
479 for (i = 0; i < newDevice->generic.device_axis_count; i++)
481 int wine_obj = newDevice->generic.axis_map[i];
483 if (wine_obj < 0) continue;
485 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
486 if (wine_obj < 8)
487 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
488 else
490 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
491 i++; /* POV takes 2 axes */
494 for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
496 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
497 df->rgodf[idx ].pguid = &GUID_Button;
498 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
500 newDevice->generic.base.data_format.wine_df = df;
502 /* initialize default properties */
503 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
504 newDevice->generic.props[i].lDevMin = -32767;
505 newDevice->generic.props[i].lDevMax = +32767;
506 newDevice->generic.props[i].lMin = 0;
507 newDevice->generic.props[i].lMax = 0xffff;
508 newDevice->generic.props[i].lDeadZone = newDevice->generic.deadzone; /* % * 1000 */
509 newDevice->generic.props[i].lSaturation = 0;
512 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
514 EnterCriticalSection(&dinput->crit);
515 list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
516 LeaveCriticalSection(&dinput->crit);
518 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
519 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
521 ddi.dwSize = sizeof(ddi);
522 fill_joystick_dideviceinstanceW(&ddi, newDevice->generic.base.dinput->dwVersion, index);
523 newDevice->generic.devcaps.dwDevType = ddi.dwDevType;
525 newDevice->generic.devcaps.dwFFSamplePeriod = 0;
526 newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
527 newDevice->generic.devcaps.dwFirmwareRevision = 0;
528 newDevice->generic.devcaps.dwHardwareRevision = 0;
529 newDevice->generic.devcaps.dwFFDriverVersion = 0;
531 if (TRACE_ON(dinput)) {
532 _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
533 for (i = 0; i < (newDevice->generic.device_axis_count); i++)
534 TRACE("axis_map[%d] = %d\n", i, newDevice->generic.axis_map[i]);
535 _dump_DIDEVCAPS(&newDevice->generic.devcaps);
538 *pdev = newDevice;
540 return DI_OK;
542 FAILED:
543 hr = DIERR_OUTOFMEMORY;
544 FAILED1:
545 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
546 HeapFree(GetProcessHeap(), 0, df);
547 release_DataFormat(&newDevice->generic.base.data_format);
548 HeapFree(GetProcessHeap(),0,newDevice->generic.axis_map);
549 HeapFree(GetProcessHeap(),0,newDevice);
550 *pdev = 0;
552 return hr;
555 /******************************************************************************
556 * get_joystick_index : Get the joystick index from a given GUID
558 static unsigned short get_joystick_index(REFGUID guid)
560 GUID wine_joystick = DInput_Wine_Joystick_GUID;
561 GUID dev_guid = *guid;
563 wine_joystick.Data3 = 0;
564 dev_guid.Data3 = 0;
566 /* for the standard joystick GUID use index 0 */
567 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
569 /* for the wine joystick GUIDs use the index stored in Data3 */
570 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
572 return MAX_JOYSTICKS;
575 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
577 unsigned short index;
579 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
580 find_joystick_devices();
581 *pdev = NULL;
583 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
584 joystick_devices_count && index < joystick_devices_count)
586 JoystickImpl *This;
587 HRESULT hr;
589 if (riid == NULL)
590 ;/* nothing */
591 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
592 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
593 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
594 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
596 unicode = 0;
598 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
599 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
600 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
601 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
603 unicode = 1;
605 else
607 WARN("no interface\n");
608 return DIERR_NOINTERFACE;
611 hr = alloc_device(rguid, dinput, &This, index);
612 if (!This) return hr;
614 if (unicode)
615 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
616 else
617 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
619 return hr;
622 return DIERR_DEVICENOTREG;
625 #undef MAX_JOYSTICKS
627 const struct dinput_device joystick_linux_device = {
628 "Wine Linux joystick driver",
629 joydev_enum_deviceA,
630 joydev_enum_deviceW,
631 joydev_create_device
634 /******************************************************************************
635 * Acquire : gets exclusive control of the joystick
637 static HRESULT WINAPI JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
639 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
640 HRESULT res;
642 TRACE("(%p)\n",This);
644 res = IDirectInputDevice2WImpl_Acquire(iface);
645 if (res != DI_OK)
646 return res;
648 /* open the joystick device */
649 if (This->joyfd==-1) {
650 TRACE("opening joystick device %s\n", This->joydev->device);
652 This->joyfd = open(This->joydev->device, O_RDONLY);
653 if (This->joyfd==-1) {
654 ERR("open(%s) failed: %s\n", This->joydev->device, strerror(errno));
655 IDirectInputDevice2WImpl_Unacquire(iface);
656 return DIERR_NOTFOUND;
660 return DI_OK;
663 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
665 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
666 return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
669 /******************************************************************************
670 * GetProperty : get input device properties
672 static HRESULT WINAPI JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
674 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
676 TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
677 _dump_DIPROPHEADER(pdiph);
679 if (!IS_DIPROP(rguid)) return DI_OK;
681 switch (LOWORD(rguid)) {
683 case (DWORD_PTR) DIPROP_VIDPID:
685 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
687 if (!This->joydev->product_id || !This->joydev->vendor_id)
688 return DIERR_UNSUPPORTED;
689 pd->dwData = MAKELONG(This->joydev->vendor_id, This->joydev->product_id);
690 TRACE("DIPROP_VIDPID(%08x)\n", pd->dwData);
691 break;
693 case (DWORD_PTR) DIPROP_JOYSTICKID:
695 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
697 pd->dwData = get_joystick_index(&This->generic.base.guid);
698 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
699 break;
702 default:
703 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
706 return DI_OK;
709 static HRESULT WINAPI JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
711 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
712 return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
715 /******************************************************************************
716 * GetDeviceInfo : get information about a device's identity
718 static HRESULT WINAPI JoystickLinuxAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface, LPDIDEVICEINSTANCEA ddi)
720 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
722 TRACE("(%p) %p\n", This, ddi);
724 if (ddi == NULL) return E_POINTER;
725 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
726 (ddi->dwSize != sizeof(DIDEVICEINSTANCEA)))
727 return DIERR_INVALIDPARAM;
729 fill_joystick_dideviceinstanceA( ddi, This->generic.base.dinput->dwVersion,
730 get_joystick_index(&This->generic.base.guid) );
731 return DI_OK;
734 static HRESULT WINAPI JoystickLinuxWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW ddi)
736 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
738 TRACE("(%p) %p\n", This, ddi);
740 if (ddi == NULL) return E_POINTER;
741 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
742 (ddi->dwSize != sizeof(DIDEVICEINSTANCEW)))
743 return DIERR_INVALIDPARAM;
745 fill_joystick_dideviceinstanceW( ddi, This->generic.base.dinput->dwVersion,
746 get_joystick_index(&This->generic.base.guid) );
747 return DI_OK;
750 /******************************************************************************
751 * Unacquire : frees the joystick
753 static HRESULT WINAPI JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
755 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
756 HRESULT res;
758 TRACE("(%p)\n",This);
760 res = IDirectInputDevice2WImpl_Unacquire(iface);
762 if (res != DI_OK)
763 return res;
765 if (This->joyfd!=-1) {
766 TRACE("closing joystick device\n");
767 close(This->joyfd);
768 This->joyfd = -1;
769 return DI_OK;
772 return DI_NOEFFECT;
775 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
777 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
778 return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
781 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
783 struct pollfd plfd;
784 struct js_event jse;
785 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
787 TRACE("(%p)\n", This);
789 if (This->joyfd==-1) {
790 WARN("no device\n");
791 return;
793 while (1)
795 LONG value;
796 int inst_id = -1;
798 plfd.fd = This->joyfd;
799 plfd.events = POLLIN;
800 if (poll(&plfd,1,0) != 1)
801 return;
802 /* we have one event, so we can read */
803 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
804 return;
806 TRACE("js_event: type 0x%x, number %d, value %d\n",
807 jse.type,jse.number,jse.value);
808 if (jse.type & JS_EVENT_BUTTON)
810 if (jse.number >= This->generic.devcaps.dwButtons) return;
812 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
813 This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
815 else if (jse.type & JS_EVENT_AXIS)
817 int number = This->generic.axis_map[jse.number]; /* wine format object index */
819 if (number < 0) return;
820 inst_id = number < 8 ? DIDFT_MAKEINSTANCE(number) | DIDFT_ABSAXIS :
821 DIDFT_MAKEINSTANCE(number - 8) | DIDFT_POV;
822 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
824 TRACE("changing axis %d => %d\n", jse.number, number);
825 switch (number)
827 case 0: This->generic.js.lX = value; break;
828 case 1: This->generic.js.lY = value; break;
829 case 2: This->generic.js.lZ = value; break;
830 case 3: This->generic.js.lRx = value; break;
831 case 4: This->generic.js.lRy = value; break;
832 case 5: This->generic.js.lRz = value; break;
833 case 6: This->generic.js.rglSlider[0] = value; break;
834 case 7: This->generic.js.rglSlider[1] = value; break;
835 case 8: case 9: case 10: case 11:
837 int idx = number - 8;
839 if (jse.number % 2)
840 This->povs[idx].y = jse.value;
841 else
842 This->povs[idx].x = jse.value;
844 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
845 break;
847 default:
848 WARN("axis %d not supported\n", number);
851 if (inst_id >= 0)
852 queue_event(iface, inst_id, value, GetCurrentTime(), This->generic.base.dinput->evsequence++);
856 static const IDirectInputDevice8AVtbl JoystickAvt =
858 IDirectInputDevice2AImpl_QueryInterface,
859 IDirectInputDevice2AImpl_AddRef,
860 IDirectInputDevice2AImpl_Release,
861 JoystickAGenericImpl_GetCapabilities,
862 IDirectInputDevice2AImpl_EnumObjects,
863 JoystickLinuxAImpl_GetProperty,
864 JoystickAGenericImpl_SetProperty,
865 JoystickLinuxAImpl_Acquire,
866 JoystickLinuxAImpl_Unacquire,
867 JoystickAGenericImpl_GetDeviceState,
868 IDirectInputDevice2AImpl_GetDeviceData,
869 IDirectInputDevice2AImpl_SetDataFormat,
870 IDirectInputDevice2AImpl_SetEventNotification,
871 IDirectInputDevice2AImpl_SetCooperativeLevel,
872 JoystickAGenericImpl_GetObjectInfo,
873 JoystickLinuxAImpl_GetDeviceInfo,
874 IDirectInputDevice2AImpl_RunControlPanel,
875 IDirectInputDevice2AImpl_Initialize,
876 IDirectInputDevice2AImpl_CreateEffect,
877 IDirectInputDevice2AImpl_EnumEffects,
878 IDirectInputDevice2AImpl_GetEffectInfo,
879 IDirectInputDevice2AImpl_GetForceFeedbackState,
880 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
881 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
882 IDirectInputDevice2AImpl_Escape,
883 JoystickAGenericImpl_Poll,
884 IDirectInputDevice2AImpl_SendDeviceData,
885 IDirectInputDevice7AImpl_EnumEffectsInFile,
886 IDirectInputDevice7AImpl_WriteEffectToFile,
887 JoystickAGenericImpl_BuildActionMap,
888 JoystickAGenericImpl_SetActionMap,
889 IDirectInputDevice8AImpl_GetImageInfo
892 static const IDirectInputDevice8WVtbl JoystickWvt =
894 IDirectInputDevice2WImpl_QueryInterface,
895 IDirectInputDevice2WImpl_AddRef,
896 IDirectInputDevice2WImpl_Release,
897 JoystickWGenericImpl_GetCapabilities,
898 IDirectInputDevice2WImpl_EnumObjects,
899 JoystickLinuxWImpl_GetProperty,
900 JoystickWGenericImpl_SetProperty,
901 JoystickLinuxWImpl_Acquire,
902 JoystickLinuxWImpl_Unacquire,
903 JoystickWGenericImpl_GetDeviceState,
904 IDirectInputDevice2WImpl_GetDeviceData,
905 IDirectInputDevice2WImpl_SetDataFormat,
906 IDirectInputDevice2WImpl_SetEventNotification,
907 IDirectInputDevice2WImpl_SetCooperativeLevel,
908 JoystickWGenericImpl_GetObjectInfo,
909 JoystickLinuxWImpl_GetDeviceInfo,
910 IDirectInputDevice2WImpl_RunControlPanel,
911 IDirectInputDevice2WImpl_Initialize,
912 IDirectInputDevice2WImpl_CreateEffect,
913 IDirectInputDevice2WImpl_EnumEffects,
914 IDirectInputDevice2WImpl_GetEffectInfo,
915 IDirectInputDevice2WImpl_GetForceFeedbackState,
916 IDirectInputDevice2WImpl_SendForceFeedbackCommand,
917 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
918 IDirectInputDevice2WImpl_Escape,
919 JoystickWGenericImpl_Poll,
920 IDirectInputDevice2WImpl_SendDeviceData,
921 IDirectInputDevice7WImpl_EnumEffectsInFile,
922 IDirectInputDevice7WImpl_WriteEffectToFile,
923 JoystickWGenericImpl_BuildActionMap,
924 JoystickWGenericImpl_SetActionMap,
925 IDirectInputDevice8WImpl_GetImageInfo
928 #else /* HAVE_LINUX_22_JOYSTICK_API */
930 const struct dinput_device joystick_linux_device = {
931 "Wine Linux joystick driver",
932 NULL,
933 NULL,
934 NULL
937 #endif /* HAVE_LINUX_22_JOYSTICK_API */