wineandroid: Create the top-level view on desktop window creation.
[wine.git] / dlls / dinput / joystick_linux.c
blob1032659621e9bf239c11396bbcf7bbbfb138f663
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 ERR("Incoherent joystick data, advertised %d axes, detected 0. Assuming 1-to-1.\n",
257 joydev.axis_count);
258 for (j = 0; j < joydev.axis_count; j++)
259 joydev.dev_axes_map[j] = j;
263 /* Find vendor_id and product_id in sysfs */
264 joydev.vendor_id = 0;
265 joydev.product_id = 0;
267 read_sys_id_variable(i, "vendor", &joydev.vendor_id);
268 read_sys_id_variable(i, "product", &joydev.product_id);
269 read_sys_id_variable(i, "bustype", &joydev.bus_type);
271 if (joydev.vendor_id == 0 || joydev.product_id == 0)
273 joydev.guid_product = DInput_Wine_Joystick_GUID;
275 else
277 /* Concatenate product_id with vendor_id to mimic Windows behaviour */
278 joydev.guid_product = DInput_Wine_Joystick_Constant_Part_GUID;
279 joydev.guid_product.Data1 = MAKELONG(joydev.vendor_id, joydev.product_id);
282 close(fd);
284 if (!joystick_devices_count)
285 new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
286 else
287 new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joystick_devices,
288 (joystick_devices_count + 1) * sizeof(struct JoyDev));
289 if (!new_joydevs) continue;
291 TRACE("Found a joystick on %s: %s\n with %d axes and %d buttons\n", joydev.device,
292 joydev.name, joydev.axis_count, joydev.button_count);
294 joystick_devices = new_joydevs;
295 joystick_devices[joystick_devices_count++] = joydev;
298 return joystick_devices_count;
301 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
303 DWORD dwSize = lpddi->dwSize;
305 TRACE("%d %p\n", dwSize, lpddi);
306 memset(lpddi, 0, dwSize);
308 /* Return joystick */
309 lpddi->dwSize = dwSize;
310 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
311 lpddi->guidInstance.Data3 = id;
312 lpddi->guidProduct = joystick_devices[id].guid_product;
313 /* we only support traditional joysticks for now */
314 if (version >= 0x0800)
315 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
316 else
317 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
319 /* Assume the joystick as HID if it is attached to USB bus and has a valid VID/PID */
320 if (joystick_devices[id].bus_type == BUS_USB &&
321 joystick_devices[id].vendor_id && joystick_devices[id].product_id)
323 lpddi->dwDevType |= DIDEVTYPE_HID;
324 lpddi->wUsagePage = 0x01; /* Desktop */
325 if (lpddi->dwDevType == DI8DEVTYPE_JOYSTICK || lpddi->dwDevType == DIDEVTYPE_JOYSTICK)
326 lpddi->wUsage = 0x04; /* Joystick */
327 else
328 lpddi->wUsage = 0x05; /* Game Pad */
331 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
332 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszProductName, MAX_PATH);
333 lpddi->guidFFDriver = GUID_NULL;
336 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
338 DIDEVICEINSTANCEW lpddiW;
339 DWORD dwSize = lpddi->dwSize;
341 lpddiW.dwSize = sizeof(lpddiW);
342 fill_joystick_dideviceinstanceW(&lpddiW, version, id);
344 TRACE("%d %p\n", dwSize, lpddi);
345 memset(lpddi, 0, dwSize);
347 /* Convert W->A */
348 lpddi->dwSize = dwSize;
349 lpddi->guidInstance = lpddiW.guidInstance;
350 lpddi->guidProduct = lpddiW.guidProduct;
351 lpddi->dwDevType = lpddiW.dwDevType;
352 strcpy(lpddi->tszInstanceName, joystick_devices[id].name);
353 strcpy(lpddi->tszProductName, joystick_devices[id].name);
354 lpddi->guidFFDriver = lpddiW.guidFFDriver;
355 lpddi->wUsagePage = lpddiW.wUsagePage;
356 lpddi->wUsage = lpddiW.wUsage;
359 static HRESULT joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
361 int fd = -1;
363 if (id >= find_joystick_devices()) return E_FAIL;
365 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
366 WARN("force feedback not supported\n");
367 return S_FALSE;
370 if ((dwDevType == 0) ||
371 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
372 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
373 /* check whether we have a joystick */
374 if ((fd = open(joystick_devices[id].device, O_RDONLY)) == -1)
376 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
377 return S_FALSE;
379 fill_joystick_dideviceinstanceA( lpddi, version, id );
380 close(fd);
381 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
382 return S_OK;
385 return S_FALSE;
388 static HRESULT joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
390 int fd = -1;
392 if (id >= find_joystick_devices()) return E_FAIL;
394 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
395 WARN("force feedback not supported\n");
396 return S_FALSE;
399 if ((dwDevType == 0) ||
400 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
401 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
402 /* check whether we have a joystick */
403 if ((fd = open(joystick_devices[id].device, O_RDONLY)) == -1)
405 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
406 return S_FALSE;
408 fill_joystick_dideviceinstanceW( lpddi, version, id );
409 close(fd);
410 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
411 return S_OK;
414 return S_FALSE;
417 static HRESULT alloc_device(REFGUID rguid, IDirectInputImpl *dinput,
418 JoystickImpl **pdev, unsigned short index)
420 DWORD i;
421 JoystickImpl* newDevice;
422 HRESULT hr;
423 LPDIDATAFORMAT df = NULL;
424 int idx = 0;
425 DIDEVICEINSTANCEW ddi;
427 TRACE("%s %p %p %hu\n", debugstr_guid(rguid), dinput, pdev, index);
429 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
430 if (newDevice == 0) {
431 WARN("out of memory\n");
432 *pdev = 0;
433 return DIERR_OUTOFMEMORY;
436 newDevice->joydev = &joystick_devices[index];
437 newDevice->joyfd = -1;
438 newDevice->generic.guidInstance = DInput_Wine_Joystick_GUID;
439 newDevice->generic.guidInstance.Data3 = index;
440 newDevice->generic.guidProduct = DInput_Wine_Joystick_GUID;
441 newDevice->generic.joy_polldev = joy_polldev;
442 newDevice->generic.name = newDevice->joydev->name;
443 newDevice->generic.device_axis_count = newDevice->joydev->axis_count;
444 newDevice->generic.devcaps.dwButtons = newDevice->joydev->button_count;
446 if (newDevice->generic.devcaps.dwButtons > 128)
448 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
449 newDevice->generic.devcaps.dwButtons = 128;
452 newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
453 newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
454 newDevice->generic.base.ref = 1;
455 newDevice->generic.base.dinput = dinput;
456 newDevice->generic.base.guid = *rguid;
457 InitializeCriticalSection(&newDevice->generic.base.crit);
458 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
460 /* setup_dinput_options may change these */
461 newDevice->generic.deadzone = 0;
463 /* do any user specified configuration */
464 hr = setup_dinput_options(&newDevice->generic, newDevice->joydev->dev_axes_map);
465 if (hr != DI_OK)
466 goto FAILED1;
468 /* Create copy of default data format */
469 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
470 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
472 df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
473 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
475 for (i = 0; i < newDevice->generic.device_axis_count; i++)
477 int wine_obj = newDevice->generic.axis_map[i];
479 if (wine_obj < 0) continue;
481 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
482 if (wine_obj < 8)
483 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
484 else
486 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
487 i++; /* POV takes 2 axes */
490 for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
492 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
493 df->rgodf[idx ].pguid = &GUID_Button;
494 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
496 newDevice->generic.base.data_format.wine_df = df;
498 /* initialize default properties */
499 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
500 newDevice->generic.props[i].lDevMin = -32767;
501 newDevice->generic.props[i].lDevMax = +32767;
502 newDevice->generic.props[i].lMin = 0;
503 newDevice->generic.props[i].lMax = 0xffff;
504 newDevice->generic.props[i].lDeadZone = newDevice->generic.deadzone; /* % * 1000 */
505 newDevice->generic.props[i].lSaturation = 0;
508 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
510 EnterCriticalSection(&dinput->crit);
511 list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
512 LeaveCriticalSection(&dinput->crit);
514 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
515 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
517 ddi.dwSize = sizeof(ddi);
518 fill_joystick_dideviceinstanceW(&ddi, newDevice->generic.base.dinput->dwVersion, index);
519 newDevice->generic.devcaps.dwDevType = ddi.dwDevType;
521 newDevice->generic.devcaps.dwFFSamplePeriod = 0;
522 newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
523 newDevice->generic.devcaps.dwFirmwareRevision = 0;
524 newDevice->generic.devcaps.dwHardwareRevision = 0;
525 newDevice->generic.devcaps.dwFFDriverVersion = 0;
527 if (TRACE_ON(dinput)) {
528 _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
529 for (i = 0; i < (newDevice->generic.device_axis_count); i++)
530 TRACE("axis_map[%d] = %d\n", i, newDevice->generic.axis_map[i]);
531 _dump_DIDEVCAPS(&newDevice->generic.devcaps);
534 *pdev = newDevice;
536 return DI_OK;
538 FAILED:
539 hr = DIERR_OUTOFMEMORY;
540 FAILED1:
541 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
542 HeapFree(GetProcessHeap(), 0, df);
543 release_DataFormat(&newDevice->generic.base.data_format);
544 HeapFree(GetProcessHeap(),0,newDevice->generic.axis_map);
545 HeapFree(GetProcessHeap(),0,newDevice);
546 *pdev = 0;
548 return hr;
551 /******************************************************************************
552 * get_joystick_index : Get the joystick index from a given GUID
554 static unsigned short get_joystick_index(REFGUID guid)
556 GUID wine_joystick = DInput_Wine_Joystick_GUID;
557 GUID dev_guid = *guid;
559 wine_joystick.Data3 = 0;
560 dev_guid.Data3 = 0;
562 /* for the standard joystick GUID use index 0 */
563 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
565 /* for the wine joystick GUIDs use the index stored in Data3 */
566 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
568 return MAX_JOYSTICKS;
571 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
573 unsigned short index;
575 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
576 find_joystick_devices();
577 *pdev = NULL;
579 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
580 joystick_devices_count && index < joystick_devices_count)
582 JoystickImpl *This;
583 HRESULT hr;
585 if (riid == NULL)
586 ;/* nothing */
587 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
588 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
589 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
590 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
592 unicode = 0;
594 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
595 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
596 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
597 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
599 unicode = 1;
601 else
603 WARN("no interface\n");
604 return DIERR_NOINTERFACE;
607 hr = alloc_device(rguid, dinput, &This, index);
608 if (!This) return hr;
610 if (unicode)
611 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
612 else
613 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
615 return hr;
618 return DIERR_DEVICENOTREG;
621 #undef MAX_JOYSTICKS
623 const struct dinput_device joystick_linux_device = {
624 "Wine Linux joystick driver",
625 joydev_enum_deviceA,
626 joydev_enum_deviceW,
627 joydev_create_device
630 /******************************************************************************
631 * Acquire : gets exclusive control of the joystick
633 static HRESULT WINAPI JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
635 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
636 HRESULT res;
638 TRACE("(%p)\n",This);
640 res = IDirectInputDevice2WImpl_Acquire(iface);
641 if (res != DI_OK)
642 return res;
644 /* open the joystick device */
645 if (This->joyfd==-1) {
646 TRACE("opening joystick device %s\n", This->joydev->device);
648 This->joyfd = open(This->joydev->device, O_RDONLY);
649 if (This->joyfd==-1) {
650 ERR("open(%s) failed: %s\n", This->joydev->device, strerror(errno));
651 IDirectInputDevice2WImpl_Unacquire(iface);
652 return DIERR_NOTFOUND;
656 return DI_OK;
659 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
661 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
662 return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
665 /******************************************************************************
666 * GetProperty : get input device properties
668 static HRESULT WINAPI JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
670 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
672 TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
673 _dump_DIPROPHEADER(pdiph);
675 if (!IS_DIPROP(rguid)) return DI_OK;
677 switch (LOWORD(rguid)) {
679 case (DWORD_PTR) DIPROP_VIDPID:
681 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
683 if (!This->joydev->product_id || !This->joydev->vendor_id)
684 return DIERR_UNSUPPORTED;
685 pd->dwData = MAKELONG(This->joydev->vendor_id, This->joydev->product_id);
686 TRACE("DIPROP_VIDPID(%08x)\n", pd->dwData);
687 break;
689 case (DWORD_PTR) DIPROP_JOYSTICKID:
691 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
693 pd->dwData = get_joystick_index(&This->generic.base.guid);
694 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
695 break;
698 default:
699 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
702 return DI_OK;
705 static HRESULT WINAPI JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
707 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
708 return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
711 /******************************************************************************
712 * GetDeviceInfo : get information about a device's identity
714 static HRESULT WINAPI JoystickLinuxAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface, LPDIDEVICEINSTANCEA ddi)
716 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
718 TRACE("(%p) %p\n", This, ddi);
720 if (ddi == NULL) return E_POINTER;
721 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
722 (ddi->dwSize != sizeof(DIDEVICEINSTANCEA)))
723 return DIERR_INVALIDPARAM;
725 fill_joystick_dideviceinstanceA( ddi, This->generic.base.dinput->dwVersion,
726 get_joystick_index(&This->generic.base.guid) );
727 return DI_OK;
730 static HRESULT WINAPI JoystickLinuxWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW ddi)
732 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
734 TRACE("(%p) %p\n", This, ddi);
736 if (ddi == NULL) return E_POINTER;
737 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
738 (ddi->dwSize != sizeof(DIDEVICEINSTANCEW)))
739 return DIERR_INVALIDPARAM;
741 fill_joystick_dideviceinstanceW( ddi, This->generic.base.dinput->dwVersion,
742 get_joystick_index(&This->generic.base.guid) );
743 return DI_OK;
746 /******************************************************************************
747 * Unacquire : frees the joystick
749 static HRESULT WINAPI JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
751 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
752 HRESULT res;
754 TRACE("(%p)\n",This);
756 res = IDirectInputDevice2WImpl_Unacquire(iface);
758 if (res != DI_OK)
759 return res;
761 if (This->joyfd!=-1) {
762 TRACE("closing joystick device\n");
763 close(This->joyfd);
764 This->joyfd = -1;
765 return DI_OK;
768 return DI_NOEFFECT;
771 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
773 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
774 return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
777 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
779 struct pollfd plfd;
780 struct js_event jse;
781 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
783 TRACE("(%p)\n", This);
785 if (This->joyfd==-1) {
786 WARN("no device\n");
787 return;
789 while (1)
791 LONG value;
792 int inst_id = -1;
794 plfd.fd = This->joyfd;
795 plfd.events = POLLIN;
796 if (poll(&plfd,1,0) != 1)
797 return;
798 /* we have one event, so we can read */
799 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
800 return;
802 TRACE("js_event: type 0x%x, number %d, value %d\n",
803 jse.type,jse.number,jse.value);
804 if (jse.type & JS_EVENT_BUTTON)
806 if (jse.number >= This->generic.devcaps.dwButtons) return;
808 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
809 This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
811 else if (jse.type & JS_EVENT_AXIS)
813 int number = This->generic.axis_map[jse.number]; /* wine format object index */
815 if (number < 0) return;
816 inst_id = number < 8 ? DIDFT_MAKEINSTANCE(number) | DIDFT_ABSAXIS :
817 DIDFT_MAKEINSTANCE(number - 8) | DIDFT_POV;
818 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
820 TRACE("changing axis %d => %d\n", jse.number, number);
821 switch (number)
823 case 0: This->generic.js.lX = value; break;
824 case 1: This->generic.js.lY = value; break;
825 case 2: This->generic.js.lZ = value; break;
826 case 3: This->generic.js.lRx = value; break;
827 case 4: This->generic.js.lRy = value; break;
828 case 5: This->generic.js.lRz = value; break;
829 case 6: This->generic.js.rglSlider[0] = value; break;
830 case 7: This->generic.js.rglSlider[1] = value; break;
831 case 8: case 9: case 10: case 11:
833 int idx = number - 8;
835 if (jse.number % 2)
836 This->povs[idx].y = jse.value;
837 else
838 This->povs[idx].x = jse.value;
840 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
841 break;
843 default:
844 WARN("axis %d not supported\n", number);
847 if (inst_id >= 0)
848 queue_event(iface, inst_id, value, GetCurrentTime(), This->generic.base.dinput->evsequence++);
852 static const IDirectInputDevice8AVtbl JoystickAvt =
854 IDirectInputDevice2AImpl_QueryInterface,
855 IDirectInputDevice2AImpl_AddRef,
856 IDirectInputDevice2AImpl_Release,
857 JoystickAGenericImpl_GetCapabilities,
858 IDirectInputDevice2AImpl_EnumObjects,
859 JoystickLinuxAImpl_GetProperty,
860 JoystickAGenericImpl_SetProperty,
861 JoystickLinuxAImpl_Acquire,
862 JoystickLinuxAImpl_Unacquire,
863 JoystickAGenericImpl_GetDeviceState,
864 IDirectInputDevice2AImpl_GetDeviceData,
865 IDirectInputDevice2AImpl_SetDataFormat,
866 IDirectInputDevice2AImpl_SetEventNotification,
867 IDirectInputDevice2AImpl_SetCooperativeLevel,
868 JoystickAGenericImpl_GetObjectInfo,
869 JoystickLinuxAImpl_GetDeviceInfo,
870 IDirectInputDevice2AImpl_RunControlPanel,
871 IDirectInputDevice2AImpl_Initialize,
872 IDirectInputDevice2AImpl_CreateEffect,
873 IDirectInputDevice2AImpl_EnumEffects,
874 IDirectInputDevice2AImpl_GetEffectInfo,
875 IDirectInputDevice2AImpl_GetForceFeedbackState,
876 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
877 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
878 IDirectInputDevice2AImpl_Escape,
879 JoystickAGenericImpl_Poll,
880 IDirectInputDevice2AImpl_SendDeviceData,
881 IDirectInputDevice7AImpl_EnumEffectsInFile,
882 IDirectInputDevice7AImpl_WriteEffectToFile,
883 JoystickAGenericImpl_BuildActionMap,
884 JoystickAGenericImpl_SetActionMap,
885 IDirectInputDevice8AImpl_GetImageInfo
888 static const IDirectInputDevice8WVtbl JoystickWvt =
890 IDirectInputDevice2WImpl_QueryInterface,
891 IDirectInputDevice2WImpl_AddRef,
892 IDirectInputDevice2WImpl_Release,
893 JoystickWGenericImpl_GetCapabilities,
894 IDirectInputDevice2WImpl_EnumObjects,
895 JoystickLinuxWImpl_GetProperty,
896 JoystickWGenericImpl_SetProperty,
897 JoystickLinuxWImpl_Acquire,
898 JoystickLinuxWImpl_Unacquire,
899 JoystickWGenericImpl_GetDeviceState,
900 IDirectInputDevice2WImpl_GetDeviceData,
901 IDirectInputDevice2WImpl_SetDataFormat,
902 IDirectInputDevice2WImpl_SetEventNotification,
903 IDirectInputDevice2WImpl_SetCooperativeLevel,
904 JoystickWGenericImpl_GetObjectInfo,
905 JoystickLinuxWImpl_GetDeviceInfo,
906 IDirectInputDevice2WImpl_RunControlPanel,
907 IDirectInputDevice2WImpl_Initialize,
908 IDirectInputDevice2WImpl_CreateEffect,
909 IDirectInputDevice2WImpl_EnumEffects,
910 IDirectInputDevice2WImpl_GetEffectInfo,
911 IDirectInputDevice2WImpl_GetForceFeedbackState,
912 IDirectInputDevice2WImpl_SendForceFeedbackCommand,
913 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
914 IDirectInputDevice2WImpl_Escape,
915 JoystickWGenericImpl_Poll,
916 IDirectInputDevice2WImpl_SendDeviceData,
917 IDirectInputDevice7WImpl_EnumEffectsInFile,
918 IDirectInputDevice7WImpl_WriteEffectToFile,
919 JoystickWGenericImpl_BuildActionMap,
920 JoystickWGenericImpl_SetActionMap,
921 IDirectInputDevice8WImpl_GetImageInfo
924 #else /* HAVE_LINUX_22_JOYSTICK_API */
926 const struct dinput_device joystick_linux_device = {
927 "Wine Linux joystick driver",
928 NULL,
929 NULL,
930 NULL
933 #endif /* HAVE_LINUX_22_JOYSTICK_API */