wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / dinput / joystick_linux.c
blob79a077a027c2e6e600eee65fae36758214a7995e
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;
601 INT i;
603 wine_joystick.Data3 = 0;
604 dev_guid.Data3 = 0;
606 /* for the standard joystick GUID use index 0 */
607 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
609 /* for the wine joystick GUIDs use the index stored in Data3 */
610 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
612 for(i = 0; i < joystick_devices_count; i++)
613 if(IsEqualGUID(&joystick_devices[i].guid_product, guid)) return i;
615 return MAX_JOYSTICKS;
618 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
620 unsigned short index;
622 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
623 find_joystick_devices();
624 *pdev = NULL;
626 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
627 joystick_devices_count && index < joystick_devices_count)
629 JoystickImpl *This;
630 HRESULT hr;
632 if (riid == NULL)
633 ;/* nothing */
634 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
635 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
636 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
637 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
639 unicode = 0;
641 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
642 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
643 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
644 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
646 unicode = 1;
648 else
650 WARN("no interface\n");
651 return DIERR_NOINTERFACE;
654 hr = alloc_device(rguid, dinput, &This, index);
655 if (!This) return hr;
657 if (unicode)
658 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
659 else
660 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
662 return hr;
665 return DIERR_DEVICENOTREG;
668 #undef MAX_JOYSTICKS
670 const struct dinput_device joystick_linux_device = {
671 "Wine Linux joystick driver",
672 joydev_enum_deviceA,
673 joydev_enum_deviceW,
674 joydev_create_device
677 /******************************************************************************
678 * Acquire : gets exclusive control of the joystick
680 static HRESULT WINAPI JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
682 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
683 HRESULT res;
685 TRACE("(%p)\n",This);
687 res = IDirectInputDevice2WImpl_Acquire(iface);
688 if (res != DI_OK)
689 return res;
691 /* open the joystick device */
692 if (This->joyfd==-1) {
693 TRACE("opening joystick device %s\n", This->joydev->device);
695 This->joyfd = open(This->joydev->device, O_RDONLY);
696 if (This->joyfd==-1) {
697 ERR("open(%s) failed: %s\n", This->joydev->device, strerror(errno));
698 IDirectInputDevice2WImpl_Unacquire(iface);
699 return DIERR_NOTFOUND;
703 return DI_OK;
706 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
708 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
709 return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
712 /******************************************************************************
713 * GetProperty : get input device properties
715 static HRESULT WINAPI JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
717 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
719 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(rguid), pdiph);
720 _dump_DIPROPHEADER(pdiph);
722 if (!IS_DIPROP(rguid)) return DI_OK;
724 switch (LOWORD(rguid)) {
726 case (DWORD_PTR) DIPROP_VIDPID:
728 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
730 if (!This->joydev->product_id || !This->joydev->vendor_id)
731 return DIERR_UNSUPPORTED;
732 pd->dwData = MAKELONG(This->joydev->vendor_id, This->joydev->product_id);
733 TRACE("DIPROP_VIDPID(%08x)\n", pd->dwData);
734 break;
736 case (DWORD_PTR) DIPROP_JOYSTICKID:
738 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
740 pd->dwData = get_joystick_index(&This->generic.base.guid);
741 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
742 break;
745 case (DWORD_PTR) DIPROP_GUIDANDPATH:
747 static const WCHAR formatW[] = {'\\','\\','?','\\','h','i','d','#','v','i','d','_','%','0','4','x','&',
748 'p','i','d','_','%','0','4','x','&','%','s','_','%','h','u',0};
749 static const WCHAR miW[] = {'m','i',0};
750 static const WCHAR igW[] = {'i','g',0};
752 BOOL is_gamepad;
753 LPDIPROPGUIDANDPATH pd = (LPDIPROPGUIDANDPATH)pdiph;
754 WORD vid = This->joydev->vendor_id;
755 WORD pid = This->joydev->product_id;
757 if (!pid || !vid)
758 return DIERR_UNSUPPORTED;
760 is_gamepad = is_xinput_device(&This->generic.devcaps, vid, pid);
761 pd->guidClass = GUID_DEVCLASS_HIDCLASS;
762 sprintfW(pd->wszPath, formatW, vid, pid, is_gamepad ? igW : miW, get_joystick_index(&This->generic.base.guid));
764 TRACE("DIPROP_GUIDANDPATH(%s, %s): returning fake path\n", debugstr_guid(&pd->guidClass), debugstr_w(pd->wszPath));
765 break;
768 default:
769 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
772 return DI_OK;
775 static HRESULT WINAPI JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
777 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
778 return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
781 /******************************************************************************
782 * GetDeviceInfo : get information about a device's identity
784 static HRESULT WINAPI JoystickLinuxAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface, LPDIDEVICEINSTANCEA ddi)
786 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
788 TRACE("(%p) %p\n", This, ddi);
790 if (ddi == NULL) return E_POINTER;
791 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
792 (ddi->dwSize != sizeof(DIDEVICEINSTANCEA)))
793 return DIERR_INVALIDPARAM;
795 fill_joystick_dideviceinstanceA( ddi, This->generic.base.dinput->dwVersion,
796 get_joystick_index(&This->generic.base.guid) );
798 ddi->guidInstance = This->generic.base.guid;
800 return DI_OK;
803 static HRESULT WINAPI JoystickLinuxWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW ddi)
805 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
807 TRACE("(%p) %p\n", This, ddi);
809 if (ddi == NULL) return E_POINTER;
810 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
811 (ddi->dwSize != sizeof(DIDEVICEINSTANCEW)))
812 return DIERR_INVALIDPARAM;
814 fill_joystick_dideviceinstanceW( ddi, This->generic.base.dinput->dwVersion,
815 get_joystick_index(&This->generic.base.guid) );
817 ddi->guidInstance = This->generic.base.guid;
819 return DI_OK;
822 /******************************************************************************
823 * Unacquire : frees the joystick
825 static HRESULT WINAPI JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
827 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
828 HRESULT res;
830 TRACE("(%p)\n",This);
832 res = IDirectInputDevice2WImpl_Unacquire(iface);
834 if (res != DI_OK)
835 return res;
837 if (This->joyfd!=-1) {
838 TRACE("closing joystick device\n");
839 close(This->joyfd);
840 This->joyfd = -1;
841 return DI_OK;
844 return DI_NOEFFECT;
847 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
849 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
850 return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
853 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
855 struct pollfd plfd;
856 struct js_event jse;
857 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
859 TRACE("(%p)\n", This);
861 if (This->joyfd==-1) {
862 WARN("no device\n");
863 return;
865 while (1)
867 LONG value;
868 int inst_id = -1;
870 plfd.fd = This->joyfd;
871 plfd.events = POLLIN;
872 if (poll(&plfd,1,0) != 1)
873 return;
874 /* we have one event, so we can read */
875 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
876 return;
878 TRACE("js_event: type 0x%x, number %d, value %d\n",
879 jse.type,jse.number,jse.value);
880 if (jse.type & JS_EVENT_BUTTON)
882 if (jse.number >= This->generic.devcaps.dwButtons) return;
884 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
885 This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
887 else if (jse.type & JS_EVENT_AXIS)
889 int number = This->generic.axis_map[jse.number]; /* wine format object index */
891 if (number < 0) return;
892 inst_id = number < 8 ? DIDFT_MAKEINSTANCE(number) | DIDFT_ABSAXIS :
893 DIDFT_MAKEINSTANCE(number - 8) | DIDFT_POV;
894 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
896 TRACE("changing axis %d => %d\n", jse.number, number);
897 switch (number)
899 case 0: This->generic.js.lX = value; break;
900 case 1: This->generic.js.lY = value; break;
901 case 2: This->generic.js.lZ = value; break;
902 case 3: This->generic.js.lRx = value; break;
903 case 4: This->generic.js.lRy = value; break;
904 case 5: This->generic.js.lRz = value; break;
905 case 6: This->generic.js.rglSlider[0] = value; break;
906 case 7: This->generic.js.rglSlider[1] = value; break;
907 case 8: case 9: case 10: case 11:
909 int idx = number - 8;
911 if (jse.number % 2)
912 This->povs[idx].y = jse.value;
913 else
914 This->povs[idx].x = jse.value;
916 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
917 break;
919 default:
920 WARN("axis %d not supported\n", number);
923 if (inst_id >= 0)
924 queue_event(iface, inst_id, value, GetCurrentTime(), This->generic.base.dinput->evsequence++);
928 static const IDirectInputDevice8AVtbl JoystickAvt =
930 IDirectInputDevice2AImpl_QueryInterface,
931 IDirectInputDevice2AImpl_AddRef,
932 IDirectInputDevice2AImpl_Release,
933 JoystickAGenericImpl_GetCapabilities,
934 IDirectInputDevice2AImpl_EnumObjects,
935 JoystickLinuxAImpl_GetProperty,
936 JoystickAGenericImpl_SetProperty,
937 JoystickLinuxAImpl_Acquire,
938 JoystickLinuxAImpl_Unacquire,
939 JoystickAGenericImpl_GetDeviceState,
940 IDirectInputDevice2AImpl_GetDeviceData,
941 IDirectInputDevice2AImpl_SetDataFormat,
942 IDirectInputDevice2AImpl_SetEventNotification,
943 IDirectInputDevice2AImpl_SetCooperativeLevel,
944 JoystickAGenericImpl_GetObjectInfo,
945 JoystickLinuxAImpl_GetDeviceInfo,
946 IDirectInputDevice2AImpl_RunControlPanel,
947 IDirectInputDevice2AImpl_Initialize,
948 IDirectInputDevice2AImpl_CreateEffect,
949 IDirectInputDevice2AImpl_EnumEffects,
950 IDirectInputDevice2AImpl_GetEffectInfo,
951 IDirectInputDevice2AImpl_GetForceFeedbackState,
952 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
953 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
954 IDirectInputDevice2AImpl_Escape,
955 JoystickAGenericImpl_Poll,
956 IDirectInputDevice2AImpl_SendDeviceData,
957 IDirectInputDevice7AImpl_EnumEffectsInFile,
958 IDirectInputDevice7AImpl_WriteEffectToFile,
959 JoystickAGenericImpl_BuildActionMap,
960 JoystickAGenericImpl_SetActionMap,
961 IDirectInputDevice8AImpl_GetImageInfo
964 static const IDirectInputDevice8WVtbl JoystickWvt =
966 IDirectInputDevice2WImpl_QueryInterface,
967 IDirectInputDevice2WImpl_AddRef,
968 IDirectInputDevice2WImpl_Release,
969 JoystickWGenericImpl_GetCapabilities,
970 IDirectInputDevice2WImpl_EnumObjects,
971 JoystickLinuxWImpl_GetProperty,
972 JoystickWGenericImpl_SetProperty,
973 JoystickLinuxWImpl_Acquire,
974 JoystickLinuxWImpl_Unacquire,
975 JoystickWGenericImpl_GetDeviceState,
976 IDirectInputDevice2WImpl_GetDeviceData,
977 IDirectInputDevice2WImpl_SetDataFormat,
978 IDirectInputDevice2WImpl_SetEventNotification,
979 IDirectInputDevice2WImpl_SetCooperativeLevel,
980 JoystickWGenericImpl_GetObjectInfo,
981 JoystickLinuxWImpl_GetDeviceInfo,
982 IDirectInputDevice2WImpl_RunControlPanel,
983 IDirectInputDevice2WImpl_Initialize,
984 IDirectInputDevice2WImpl_CreateEffect,
985 IDirectInputDevice2WImpl_EnumEffects,
986 IDirectInputDevice2WImpl_GetEffectInfo,
987 IDirectInputDevice2WImpl_GetForceFeedbackState,
988 IDirectInputDevice2WImpl_SendForceFeedbackCommand,
989 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
990 IDirectInputDevice2WImpl_Escape,
991 JoystickWGenericImpl_Poll,
992 IDirectInputDevice2WImpl_SendDeviceData,
993 IDirectInputDevice7WImpl_EnumEffectsInFile,
994 IDirectInputDevice7WImpl_WriteEffectToFile,
995 JoystickWGenericImpl_BuildActionMap,
996 JoystickWGenericImpl_SetActionMap,
997 IDirectInputDevice8WImpl_GetImageInfo
1000 #else /* HAVE_LINUX_22_JOYSTICK_API */
1002 const struct dinput_device joystick_linux_device = {
1003 "Wine Linux joystick driver",
1004 NULL,
1005 NULL,
1006 NULL
1009 #endif /* HAVE_LINUX_22_JOYSTICK_API */