wineconsole: Leave room for wide characters only when using a DBCS codepage.
[wine.git] / dlls / dinput / joystick_linux.c
blob1f8f94d7382f7ee026e30c510a85649b3ef740fd
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;
233 /* Remap to DI numbers */
234 for (j = 0; j < joydev.axis_count; j++)
235 if (axes_map[j] < 8)
236 /* Axis match 1-to-1 */
237 joydev.dev_axes_map[j] = j;
238 else if (axes_map[j] == 16 ||
239 axes_map[j] == 17)
240 /* POV axis */
241 joydev.dev_axes_map[j] = 8;
242 else
243 joydev.dev_axes_map[j] = -1;
246 /* Find vendor_id and product_id in sysfs */
247 joydev.vendor_id = 0;
248 joydev.product_id = 0;
250 read_sys_id_variable(i, "vendor", &joydev.vendor_id);
251 read_sys_id_variable(i, "product", &joydev.product_id);
252 read_sys_id_variable(i, "bustype", &joydev.bus_type);
254 if (joydev.vendor_id == 0 || joydev.product_id == 0)
256 joydev.guid_product = DInput_Wine_Joystick_GUID;
258 else
260 /* Concatenate product_id with vendor_id to mimic Windows behaviour */
261 joydev.guid_product = DInput_Wine_Joystick_Constant_Part_GUID;
262 joydev.guid_product.Data1 = MAKELONG(joydev.vendor_id, joydev.product_id);
265 close(fd);
267 if (!joystick_devices_count)
268 new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
269 else
270 new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joystick_devices,
271 (joystick_devices_count + 1) * sizeof(struct JoyDev));
272 if (!new_joydevs) continue;
274 TRACE("Found a joystick on %s: %s\n with %d axes and %d buttons\n", joydev.device,
275 joydev.name, joydev.axis_count, joydev.button_count);
277 joystick_devices = new_joydevs;
278 joystick_devices[joystick_devices_count++] = joydev;
281 return joystick_devices_count;
284 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
286 DWORD dwSize = lpddi->dwSize;
288 TRACE("%d %p\n", dwSize, lpddi);
289 memset(lpddi, 0, dwSize);
291 /* Return joystick */
292 lpddi->dwSize = dwSize;
293 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
294 lpddi->guidInstance.Data3 = id;
295 lpddi->guidProduct = joystick_devices[id].guid_product;
296 /* we only support traditional joysticks for now */
297 if (version >= 0x0800)
298 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
299 else
300 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
302 /* Assume the joystick as HID if it is attached to USB bus and has a valid VID/PID */
303 if (joystick_devices[id].bus_type == BUS_USB &&
304 joystick_devices[id].vendor_id && joystick_devices[id].product_id)
306 lpddi->dwDevType |= DIDEVTYPE_HID;
307 lpddi->wUsagePage = 0x01; /* Desktop */
308 if (lpddi->dwDevType == DI8DEVTYPE_JOYSTICK || lpddi->dwDevType == DIDEVTYPE_JOYSTICK)
309 lpddi->wUsage = 0x04; /* Joystick */
310 else
311 lpddi->wUsage = 0x05; /* Game Pad */
314 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
315 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszProductName, MAX_PATH);
316 lpddi->guidFFDriver = GUID_NULL;
319 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
321 DIDEVICEINSTANCEW lpddiW;
322 DWORD dwSize = lpddi->dwSize;
324 lpddiW.dwSize = sizeof(lpddiW);
325 fill_joystick_dideviceinstanceW(&lpddiW, version, id);
327 TRACE("%d %p\n", dwSize, lpddi);
328 memset(lpddi, 0, dwSize);
330 /* Convert W->A */
331 lpddi->dwSize = dwSize;
332 lpddi->guidInstance = lpddiW.guidInstance;
333 lpddi->guidProduct = lpddiW.guidProduct;
334 lpddi->dwDevType = lpddiW.dwDevType;
335 strcpy(lpddi->tszInstanceName, joystick_devices[id].name);
336 strcpy(lpddi->tszProductName, joystick_devices[id].name);
337 lpddi->guidFFDriver = lpddiW.guidFFDriver;
338 lpddi->wUsagePage = lpddiW.wUsagePage;
339 lpddi->wUsage = lpddiW.wUsage;
342 static HRESULT joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
344 int fd = -1;
346 if (id >= find_joystick_devices()) return E_FAIL;
348 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
349 WARN("force feedback not supported\n");
350 return S_FALSE;
353 if ((dwDevType == 0) ||
354 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
355 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
356 /* check whether we have a joystick */
357 if ((fd = open(joystick_devices[id].device, O_RDONLY)) == -1)
359 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
360 return S_FALSE;
362 fill_joystick_dideviceinstanceA( lpddi, version, id );
363 close(fd);
364 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
365 return S_OK;
368 return S_FALSE;
371 static HRESULT joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
373 int fd = -1;
375 if (id >= find_joystick_devices()) return E_FAIL;
377 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
378 WARN("force feedback not supported\n");
379 return S_FALSE;
382 if ((dwDevType == 0) ||
383 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
384 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
385 /* check whether we have a joystick */
386 if ((fd = open(joystick_devices[id].device, O_RDONLY)) == -1)
388 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
389 return S_FALSE;
391 fill_joystick_dideviceinstanceW( lpddi, version, id );
392 close(fd);
393 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
394 return S_OK;
397 return S_FALSE;
400 static HRESULT alloc_device(REFGUID rguid, IDirectInputImpl *dinput,
401 JoystickImpl **pdev, unsigned short index)
403 DWORD i;
404 JoystickImpl* newDevice;
405 HRESULT hr;
406 LPDIDATAFORMAT df = NULL;
407 int idx = 0;
408 DIDEVICEINSTANCEW ddi;
410 TRACE("%s %p %p %hu\n", debugstr_guid(rguid), dinput, pdev, index);
412 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
413 if (newDevice == 0) {
414 WARN("out of memory\n");
415 *pdev = 0;
416 return DIERR_OUTOFMEMORY;
419 newDevice->joydev = &joystick_devices[index];
420 newDevice->joyfd = -1;
421 newDevice->generic.guidInstance = DInput_Wine_Joystick_GUID;
422 newDevice->generic.guidInstance.Data3 = index;
423 newDevice->generic.guidProduct = DInput_Wine_Joystick_GUID;
424 newDevice->generic.joy_polldev = joy_polldev;
425 newDevice->generic.name = newDevice->joydev->name;
426 newDevice->generic.device_axis_count = newDevice->joydev->axis_count;
427 newDevice->generic.devcaps.dwButtons = newDevice->joydev->button_count;
429 if (newDevice->generic.devcaps.dwButtons > 128)
431 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
432 newDevice->generic.devcaps.dwButtons = 128;
435 newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
436 newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
437 newDevice->generic.base.ref = 1;
438 newDevice->generic.base.dinput = dinput;
439 newDevice->generic.base.guid = *rguid;
440 InitializeCriticalSection(&newDevice->generic.base.crit);
441 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
443 /* setup_dinput_options may change these */
444 newDevice->generic.deadzone = 0;
446 /* do any user specified configuration */
447 hr = setup_dinput_options(&newDevice->generic, newDevice->joydev->dev_axes_map);
448 if (hr != DI_OK)
449 goto FAILED1;
451 /* Create copy of default data format */
452 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
453 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
455 df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
456 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
458 for (i = 0; i < newDevice->generic.device_axis_count; i++)
460 int wine_obj = newDevice->generic.axis_map[i];
462 if (wine_obj < 0) continue;
464 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
465 if (wine_obj < 8)
466 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
467 else
469 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
470 i++; /* POV takes 2 axes */
473 for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
475 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
476 df->rgodf[idx ].pguid = &GUID_Button;
477 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
479 newDevice->generic.base.data_format.wine_df = df;
481 /* initialize default properties */
482 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
483 newDevice->generic.props[i].lDevMin = -32767;
484 newDevice->generic.props[i].lDevMax = +32767;
485 newDevice->generic.props[i].lMin = 0;
486 newDevice->generic.props[i].lMax = 0xffff;
487 newDevice->generic.props[i].lDeadZone = newDevice->generic.deadzone; /* % * 1000 */
488 newDevice->generic.props[i].lSaturation = 0;
491 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
493 EnterCriticalSection(&dinput->crit);
494 list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
495 LeaveCriticalSection(&dinput->crit);
497 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
498 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
500 ddi.dwSize = sizeof(ddi);
501 fill_joystick_dideviceinstanceW(&ddi, newDevice->generic.base.dinput->dwVersion, index);
502 newDevice->generic.devcaps.dwDevType = ddi.dwDevType;
504 newDevice->generic.devcaps.dwFFSamplePeriod = 0;
505 newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
506 newDevice->generic.devcaps.dwFirmwareRevision = 0;
507 newDevice->generic.devcaps.dwHardwareRevision = 0;
508 newDevice->generic.devcaps.dwFFDriverVersion = 0;
510 if (TRACE_ON(dinput)) {
511 _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
512 for (i = 0; i < (newDevice->generic.device_axis_count); i++)
513 TRACE("axis_map[%d] = %d\n", i, newDevice->generic.axis_map[i]);
514 _dump_DIDEVCAPS(&newDevice->generic.devcaps);
517 *pdev = newDevice;
519 return DI_OK;
521 FAILED:
522 hr = DIERR_OUTOFMEMORY;
523 FAILED1:
524 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
525 HeapFree(GetProcessHeap(), 0, df);
526 release_DataFormat(&newDevice->generic.base.data_format);
527 HeapFree(GetProcessHeap(),0,newDevice->generic.axis_map);
528 HeapFree(GetProcessHeap(),0,newDevice);
529 *pdev = 0;
531 return hr;
534 /******************************************************************************
535 * get_joystick_index : Get the joystick index from a given GUID
537 static unsigned short get_joystick_index(REFGUID guid)
539 GUID wine_joystick = DInput_Wine_Joystick_GUID;
540 GUID dev_guid = *guid;
542 wine_joystick.Data3 = 0;
543 dev_guid.Data3 = 0;
545 /* for the standard joystick GUID use index 0 */
546 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
548 /* for the wine joystick GUIDs use the index stored in Data3 */
549 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
551 return MAX_JOYSTICKS;
554 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
556 unsigned short index;
558 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
559 find_joystick_devices();
560 *pdev = NULL;
562 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
563 joystick_devices_count && index < joystick_devices_count)
565 JoystickImpl *This;
566 HRESULT hr;
568 if (riid == NULL)
569 ;/* nothing */
570 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
571 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
572 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
573 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
575 unicode = 0;
577 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
578 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
579 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
580 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
582 unicode = 1;
584 else
586 WARN("no interface\n");
587 return DIERR_NOINTERFACE;
590 hr = alloc_device(rguid, dinput, &This, index);
591 if (!This) return hr;
593 if (unicode)
594 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
595 else
596 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
598 return hr;
601 return DIERR_DEVICENOTREG;
604 #undef MAX_JOYSTICKS
606 const struct dinput_device joystick_linux_device = {
607 "Wine Linux joystick driver",
608 joydev_enum_deviceA,
609 joydev_enum_deviceW,
610 joydev_create_device
613 /******************************************************************************
614 * Acquire : gets exclusive control of the joystick
616 static HRESULT WINAPI JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
618 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
619 HRESULT res;
621 TRACE("(%p)\n",This);
623 res = IDirectInputDevice2WImpl_Acquire(iface);
624 if (res != DI_OK)
625 return res;
627 /* open the joystick device */
628 if (This->joyfd==-1) {
629 TRACE("opening joystick device %s\n", This->joydev->device);
631 This->joyfd = open(This->joydev->device, O_RDONLY);
632 if (This->joyfd==-1) {
633 ERR("open(%s) failed: %s\n", This->joydev->device, strerror(errno));
634 IDirectInputDevice2WImpl_Unacquire(iface);
635 return DIERR_NOTFOUND;
639 return DI_OK;
642 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
644 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
645 return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
648 /******************************************************************************
649 * GetProperty : get input device properties
651 static HRESULT WINAPI JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
653 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
655 TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
656 _dump_DIPROPHEADER(pdiph);
658 if (!IS_DIPROP(rguid)) return DI_OK;
660 switch (LOWORD(rguid)) {
662 case (DWORD_PTR) DIPROP_VIDPID:
664 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
666 if (!This->joydev->product_id || !This->joydev->vendor_id)
667 return DIERR_UNSUPPORTED;
668 pd->dwData = MAKELONG(This->joydev->vendor_id, This->joydev->product_id);
669 TRACE("DIPROP_VIDPID(%08x)\n", pd->dwData);
670 break;
672 case (DWORD_PTR) DIPROP_JOYSTICKID:
674 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
676 pd->dwData = get_joystick_index(&This->generic.base.guid);
677 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
678 break;
681 default:
682 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
685 return DI_OK;
688 static HRESULT WINAPI JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
690 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
691 return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
694 /******************************************************************************
695 * GetDeviceInfo : get information about a device's identity
697 static HRESULT WINAPI JoystickLinuxAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface, LPDIDEVICEINSTANCEA ddi)
699 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
701 TRACE("(%p) %p\n", This, ddi);
703 if (ddi == NULL) return E_POINTER;
704 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
705 (ddi->dwSize != sizeof(DIDEVICEINSTANCEA)))
706 return DIERR_INVALIDPARAM;
708 fill_joystick_dideviceinstanceA( ddi, This->generic.base.dinput->dwVersion,
709 get_joystick_index(&This->generic.base.guid) );
710 return DI_OK;
713 static HRESULT WINAPI JoystickLinuxWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW ddi)
715 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
717 TRACE("(%p) %p\n", This, ddi);
719 if (ddi == NULL) return E_POINTER;
720 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
721 (ddi->dwSize != sizeof(DIDEVICEINSTANCEW)))
722 return DIERR_INVALIDPARAM;
724 fill_joystick_dideviceinstanceW( ddi, This->generic.base.dinput->dwVersion,
725 get_joystick_index(&This->generic.base.guid) );
726 return DI_OK;
729 /******************************************************************************
730 * Unacquire : frees the joystick
732 static HRESULT WINAPI JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
734 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
735 HRESULT res;
737 TRACE("(%p)\n",This);
739 res = IDirectInputDevice2WImpl_Unacquire(iface);
741 if (res != DI_OK)
742 return res;
744 if (This->joyfd!=-1) {
745 TRACE("closing joystick device\n");
746 close(This->joyfd);
747 This->joyfd = -1;
748 return DI_OK;
751 return DI_NOEFFECT;
754 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
756 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
757 return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
760 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
762 struct pollfd plfd;
763 struct js_event jse;
764 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
766 TRACE("(%p)\n", This);
768 if (This->joyfd==-1) {
769 WARN("no device\n");
770 return;
772 while (1)
774 LONG value;
775 int inst_id = -1;
777 plfd.fd = This->joyfd;
778 plfd.events = POLLIN;
779 if (poll(&plfd,1,0) != 1)
780 return;
781 /* we have one event, so we can read */
782 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
783 return;
785 TRACE("js_event: type 0x%x, number %d, value %d\n",
786 jse.type,jse.number,jse.value);
787 if (jse.type & JS_EVENT_BUTTON)
789 if (jse.number >= This->generic.devcaps.dwButtons) return;
791 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
792 This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
794 else if (jse.type & JS_EVENT_AXIS)
796 int number = This->generic.axis_map[jse.number]; /* wine format object index */
798 if (number < 0) return;
799 inst_id = number < 8 ? DIDFT_MAKEINSTANCE(number) | DIDFT_ABSAXIS :
800 DIDFT_MAKEINSTANCE(number - 8) | DIDFT_POV;
801 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
803 TRACE("changing axis %d => %d\n", jse.number, number);
804 switch (number)
806 case 0: This->generic.js.lX = value; break;
807 case 1: This->generic.js.lY = value; break;
808 case 2: This->generic.js.lZ = value; break;
809 case 3: This->generic.js.lRx = value; break;
810 case 4: This->generic.js.lRy = value; break;
811 case 5: This->generic.js.lRz = value; break;
812 case 6: This->generic.js.rglSlider[0] = value; break;
813 case 7: This->generic.js.rglSlider[1] = value; break;
814 case 8: case 9: case 10: case 11:
816 int idx = number - 8;
818 if (jse.number % 2)
819 This->povs[idx].y = jse.value;
820 else
821 This->povs[idx].x = jse.value;
823 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
824 break;
826 default:
827 WARN("axis %d not supported\n", number);
830 if (inst_id >= 0)
831 queue_event(iface, inst_id, value, GetCurrentTime(), This->generic.base.dinput->evsequence++);
835 static const IDirectInputDevice8AVtbl JoystickAvt =
837 IDirectInputDevice2AImpl_QueryInterface,
838 IDirectInputDevice2AImpl_AddRef,
839 IDirectInputDevice2AImpl_Release,
840 JoystickAGenericImpl_GetCapabilities,
841 IDirectInputDevice2AImpl_EnumObjects,
842 JoystickLinuxAImpl_GetProperty,
843 JoystickAGenericImpl_SetProperty,
844 JoystickLinuxAImpl_Acquire,
845 JoystickLinuxAImpl_Unacquire,
846 JoystickAGenericImpl_GetDeviceState,
847 IDirectInputDevice2AImpl_GetDeviceData,
848 IDirectInputDevice2AImpl_SetDataFormat,
849 IDirectInputDevice2AImpl_SetEventNotification,
850 IDirectInputDevice2AImpl_SetCooperativeLevel,
851 JoystickAGenericImpl_GetObjectInfo,
852 JoystickLinuxAImpl_GetDeviceInfo,
853 IDirectInputDevice2AImpl_RunControlPanel,
854 IDirectInputDevice2AImpl_Initialize,
855 IDirectInputDevice2AImpl_CreateEffect,
856 IDirectInputDevice2AImpl_EnumEffects,
857 IDirectInputDevice2AImpl_GetEffectInfo,
858 IDirectInputDevice2AImpl_GetForceFeedbackState,
859 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
860 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
861 IDirectInputDevice2AImpl_Escape,
862 JoystickAGenericImpl_Poll,
863 IDirectInputDevice2AImpl_SendDeviceData,
864 IDirectInputDevice7AImpl_EnumEffectsInFile,
865 IDirectInputDevice7AImpl_WriteEffectToFile,
866 JoystickAGenericImpl_BuildActionMap,
867 JoystickAGenericImpl_SetActionMap,
868 IDirectInputDevice8AImpl_GetImageInfo
871 static const IDirectInputDevice8WVtbl JoystickWvt =
873 IDirectInputDevice2WImpl_QueryInterface,
874 IDirectInputDevice2WImpl_AddRef,
875 IDirectInputDevice2WImpl_Release,
876 JoystickWGenericImpl_GetCapabilities,
877 IDirectInputDevice2WImpl_EnumObjects,
878 JoystickLinuxWImpl_GetProperty,
879 JoystickWGenericImpl_SetProperty,
880 JoystickLinuxWImpl_Acquire,
881 JoystickLinuxWImpl_Unacquire,
882 JoystickWGenericImpl_GetDeviceState,
883 IDirectInputDevice2WImpl_GetDeviceData,
884 IDirectInputDevice2WImpl_SetDataFormat,
885 IDirectInputDevice2WImpl_SetEventNotification,
886 IDirectInputDevice2WImpl_SetCooperativeLevel,
887 JoystickWGenericImpl_GetObjectInfo,
888 JoystickLinuxWImpl_GetDeviceInfo,
889 IDirectInputDevice2WImpl_RunControlPanel,
890 IDirectInputDevice2WImpl_Initialize,
891 IDirectInputDevice2WImpl_CreateEffect,
892 IDirectInputDevice2WImpl_EnumEffects,
893 IDirectInputDevice2WImpl_GetEffectInfo,
894 IDirectInputDevice2WImpl_GetForceFeedbackState,
895 IDirectInputDevice2WImpl_SendForceFeedbackCommand,
896 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
897 IDirectInputDevice2WImpl_Escape,
898 JoystickWGenericImpl_Poll,
899 IDirectInputDevice2WImpl_SendDeviceData,
900 IDirectInputDevice7WImpl_EnumEffectsInFile,
901 IDirectInputDevice7WImpl_WriteEffectToFile,
902 JoystickWGenericImpl_BuildActionMap,
903 JoystickWGenericImpl_SetActionMap,
904 IDirectInputDevice8WImpl_GetImageInfo
907 #else /* HAVE_LINUX_22_JOYSTICK_API */
909 const struct dinput_device joystick_linux_device = {
910 "Wine Linux joystick driver",
911 NULL,
912 NULL,
913 NULL
916 #endif /* HAVE_LINUX_22_JOYSTICK_API */