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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
25 #ifdef HAVE_LINUX_22_JOYSTICK_API
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
38 #include <sys/fcntl.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 # include <sys/ioctl.h>
43 #ifdef HAVE_SYS_ERRNO_H
44 # include <sys/errno.h>
46 #ifdef HAVE_LINUX_IOCTL_H
47 # include <linux/ioctl.h>
49 #ifdef HAVE_LINUX_JOYSTICK_H
50 # include <linux/joystick.h>
52 #define JOYDEV "/dev/js0"
54 #include "wine/debug.h"
55 #include "wine/unicode.h"
61 #include "dinput_private.h"
62 #include "device_private.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
66 /* Wine joystick driver object instances */
67 #define WINE_JOYSTICK_AXIS_BASE 0
68 #define WINE_JOYSTICK_BUTTON_BASE 8
77 typedef struct JoystickImpl JoystickImpl;
78 static IDirectInputDevice8AVtbl JoystickAvt;
79 static IDirectInputDevice8WVtbl JoystickWvt;
86 /* The 'parent' DInput */
87 IDirectInputImpl *dinput;
89 /* joystick private */
91 DIJOYSTATE2 js; /* wine data */
92 LPDIDATAFORMAT user_df; /* user defined format */
93 DataFormat *transform; /* wine to user format converter */
94 int *offsets; /* object offsets */
97 LPDIDEVICEOBJECTDATA data_queue;
98 int queue_head, queue_tail, queue_len;
103 static GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
107 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
110 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, int version)
114 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
115 WARN("force feedback not supported\n");
119 if ((dwDevType==0) || (GET_DIDEVICE_TYPE(dwDevType)==DIDEVTYPE_JOYSTICK)) {
120 /* check whether we have a joystick */
121 if ((fd = open(JOYDEV,O_RDONLY)) < 0) {
122 WARN("open(%s,O_RDONLY) failed: %s\n", JOYDEV, strerror(errno));
126 /* Return joystick */
127 lpddi->guidInstance = GUID_Joystick;
128 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
129 /* we only support traditional joysticks for now */
131 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
133 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
134 strcpy(lpddi->tszInstanceName, "Joystick");
135 #if defined(JSIOCGNAME)
136 if (ioctl(fd,JSIOCGNAME(sizeof(lpddi->tszProductName)),lpddi->tszProductName) < 0) {
137 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", JOYDEV, strerror(errno));
138 strcpy(lpddi->tszProductName, "Wine Joystick");
141 strcpy(lpddi->tszProductName, "Wine Joystick");
144 lpddi->guidFFDriver = GUID_NULL;
146 TRACE("Enumerating the linux Joystick device: %s (%s)\n", JOYDEV, lpddi->tszProductName);
153 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, int version)
158 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
159 WARN("force feedback not supported\n");
163 if ((dwDevType==0) || (GET_DIDEVICE_TYPE(dwDevType)==DIDEVTYPE_JOYSTICK)) {
164 /* check whether we have a joystick */
165 if ((fd = open(JOYDEV,O_RDONLY) < 0)) {
166 WARN("open(%s,O_RDONLY) failed: %s\n", JOYDEV, strerror(errno));
170 /* Return joystick */
171 lpddi->guidInstance = GUID_Joystick;
172 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
173 /* we only support traditional joysticks for now */
174 lpddi->dwDevType = DIDEVTYPE_JOYSTICK |
175 (DIDEVTYPEJOYSTICK_TRADITIONAL<<8);
177 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, lpddi->tszInstanceName, MAX_PATH);
178 #if defined(JSIOCGNAME)
179 if (ioctl(fd,JSIOCGNAME(sizeof(name)),name) < 0) {
180 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", JOYDEV, strerror(errno));
181 strcpy(name, "Wine Joystick");
184 strcpy(name, "Wine Joystick");
186 MultiByteToWideChar(CP_ACP, 0, name, -1, lpddi->tszProductName, MAX_PATH);
187 lpddi->guidFFDriver = GUID_NULL;
189 TRACE("Enumerating the linux Joystick device: %s (%s)\n",JOYDEV,name);
196 static JoystickImpl *alloc_device(REFGUID rguid, LPVOID jvt, IDirectInputImpl *dinput)
199 JoystickImpl* newDevice;
201 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
205 newDevice->lpVtbl = jvt;
207 newDevice->joyfd = -1;
208 newDevice->dinput = dinput;
209 newDevice->acquired = FALSE;
210 newDevice->name = NULL;
211 CopyMemory(&(newDevice->guid),rguid,sizeof(*rguid));
213 /* wine uses DIJOYSTATE2 as it's internal format so copy
214 * the already defined format c_dfDIJoystick2 */
215 newDevice->user_df = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwSize);
216 if (newDevice->user_df == 0)
219 CopyMemory(newDevice->user_df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
221 /* copy default objects */
222 newDevice->user_df->rgodf = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*c_dfDIJoystick2.dwObjSize);
223 if (newDevice->user_df->rgodf == 0)
226 CopyMemory(newDevice->user_df->rgodf,c_dfDIJoystick2.rgodf,c_dfDIJoystick2.dwNumObjs*c_dfDIJoystick2.dwObjSize);
228 /* create default properties */
229 newDevice->props = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*sizeof(ObjProps));
230 if (newDevice->props == 0)
233 /* initialize default properties */
234 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
235 newDevice->props[i].lMin = 0;
236 newDevice->props[i].lMax = 0xffff;
237 newDevice->props[i].lDeadZone = 1000;
238 newDevice->props[i].lSaturation = 0;
241 /* create an offsets array */
242 newDevice->offsets = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,c_dfDIJoystick2.dwNumObjs*sizeof(int));
243 if (newDevice->offsets == 0)
246 /* create the default transform filter */
247 newDevice->transform = create_DataFormat(&c_dfDIJoystick2, newDevice->user_df, newDevice->offsets);
249 IDirectInputDevice_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->dinput);
251 if (TRACE_ON(dinput))
252 _dump_DIDATAFORMAT(newDevice->user_df);
257 if (newDevice->props)
258 HeapFree(GetProcessHeap(),0,newDevice->props);
259 if (newDevice->user_df->rgodf)
260 HeapFree(GetProcessHeap(),0,newDevice->user_df->rgodf);
261 if (newDevice->user_df)
262 HeapFree(GetProcessHeap(),0,newDevice->user_df);
264 HeapFree(GetProcessHeap(),0,newDevice);
268 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
270 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
271 (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) {
272 if ((riid == NULL) ||
273 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
274 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
275 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
276 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
277 *pdev=(IDirectInputDeviceA*) alloc_device(rguid, &JoystickAvt, dinput);
279 WARN("out of memory\n");
280 return DIERR_OUTOFMEMORY;
283 TRACE("Creating a Joystick device (%p)\n", *pdev);
286 WARN("no interface\n");
288 return DIERR_NOINTERFACE;
292 WARN("invalid device GUID\n");
294 return DIERR_DEVICENOTREG;
297 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
299 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
300 (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) {
301 if ((riid == NULL) ||
302 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
303 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
304 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
305 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
306 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &JoystickWvt, dinput);
308 WARN("out of memory\n");
309 return DIERR_OUTOFMEMORY;
312 TRACE("Creating a Joystick device (%p)\n", *pdev);
315 WARN("no interface\n");
317 return DIERR_NOINTERFACE;
321 WARN("invalid device GUID\n");
323 return DIERR_DEVICENOTREG;
326 static dinput_device joydev = {
328 "Wine Linux joystick driver",
331 joydev_create_deviceA,
332 joydev_create_deviceW
335 DECL_GLOBAL_CONSTRUCTOR(joydev_register) { dinput_register_device(&joydev); }
337 /******************************************************************************
340 static ULONG WINAPI JoystickAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
342 ICOM_THIS(JoystickImpl,iface);
348 /* Free the device name */
350 HeapFree(GetProcessHeap(),0,This->name);
352 /* Free the data queue */
353 if (This->data_queue != NULL)
354 HeapFree(GetProcessHeap(),0,This->data_queue);
356 /* Free the DataFormat */
357 HeapFree(GetProcessHeap(), 0, This->user_df->rgodf);
358 HeapFree(GetProcessHeap(), 0, This->user_df);
360 /* Free the properties */
361 HeapFree(GetProcessHeap(), 0, This->props);
363 /* Free the offsets array */
364 HeapFree(GetProcessHeap(),0,This->offsets);
366 /* release the data transform filter */
367 release_DataFormat(This->transform);
369 IDirectInputDevice_Release((LPDIRECTINPUTDEVICE8A)This->dinput);
371 HeapFree(GetProcessHeap(),0,This);
375 /******************************************************************************
376 * SetDataFormat : the application can choose the format of the data
377 * the device driver sends back with GetDeviceState.
379 static HRESULT WINAPI JoystickAImpl_SetDataFormat(
380 LPDIRECTINPUTDEVICE8A iface,
383 ICOM_THIS(JoystickImpl,iface);
385 LPDIDATAFORMAT new_df = 0;
386 LPDIOBJECTDATAFORMAT new_rgodf = 0;
387 ObjProps * new_props = 0;
388 int * new_offsets = 0;
390 TRACE("(%p,%p)\n",This,df);
392 if (This->acquired) {
394 return DIERR_ACQUIRED;
397 if (TRACE_ON(dinput))
398 _dump_DIDATAFORMAT(df);
400 /* Store the new data format */
401 new_df = HeapAlloc(GetProcessHeap(),0,df->dwSize);
405 new_rgodf = HeapAlloc(GetProcessHeap(),0,df->dwNumObjs*df->dwObjSize);
409 new_props = HeapAlloc(GetProcessHeap(),0,df->dwNumObjs*sizeof(ObjProps));
413 new_offsets = HeapAlloc(GetProcessHeap(),0,df->dwNumObjs*sizeof(int));
414 if (new_offsets == 0)
417 HeapFree(GetProcessHeap(),0,This->user_df);
418 HeapFree(GetProcessHeap(),0,This->user_df->rgodf);
419 HeapFree(GetProcessHeap(),0,This->props);
420 HeapFree(GetProcessHeap(),0,This->offsets);
421 release_DataFormat(This->transform);
423 This->user_df = new_df;
424 CopyMemory(This->user_df, df, df->dwSize);
425 This->user_df->rgodf = new_rgodf;
426 CopyMemory(This->user_df->rgodf,df->rgodf,df->dwNumObjs*df->dwObjSize);
427 This->props = new_props;
428 for (i = 0; i < df->dwNumObjs; i++) {
429 This->props[i].lMin = 0;
430 This->props[i].lMax = 0xffff;
431 This->props[i].lDeadZone = 1000;
432 This->props[i].lSaturation = 0;
434 This->offsets = new_offsets;
435 This->transform = create_DataFormat(&c_dfDIJoystick2, This->user_df, This->offsets);
440 WARN("out of memory\n");
442 HeapFree(GetProcessHeap(),0,new_offsets);
444 HeapFree(GetProcessHeap(),0,new_props);
446 HeapFree(GetProcessHeap(),0,new_rgodf);
448 HeapFree(GetProcessHeap(),0,new_df);
449 return DIERR_OUTOFMEMORY;
452 /******************************************************************************
453 * Acquire : gets exclusive control of the joystick
455 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
457 ICOM_THIS(JoystickImpl,iface);
459 TRACE("(%p)\n",This);
461 if (This->acquired) {
462 WARN("already acquired\n");
466 /* open the joystick device */
467 if (This->joyfd==-1) {
468 TRACE("opening joystick device %s\n", JOYDEV);
470 This->joyfd=open(JOYDEV,O_RDONLY);
471 if (This->joyfd==-1) {
472 ERR("open(%s) failed: %s\n", JOYDEV, strerror(errno));
473 return DIERR_NOTFOUND;
477 This->acquired = TRUE;
482 /******************************************************************************
483 * Unacquire : frees the joystick
485 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
487 ICOM_THIS(JoystickImpl,iface);
489 TRACE("(%p)\n",This);
491 if (!This->acquired) {
492 WARN("not acquired\n");
493 return DIERR_NOTACQUIRED;
496 if (This->joyfd!=-1) {
497 TRACE("closing joystick device\n");
500 This->acquired = FALSE;
504 This->acquired = FALSE;
509 LONG map_axis(JoystickImpl * This, short val, short index)
512 double fmin = This->props[index].lMin;
513 double fmax = This->props[index].lMax;
516 fret = (((fval + 32767.0) * (fmax - fmin)) / (32767.0*2.0)) + fmin;
526 /* convert user format offset to user format object index */
527 int offset_to_object(JoystickImpl *This, int offset)
531 for (i = 0; i < This->user_df->dwNumObjs; i++) {
532 if (This->user_df->rgodf[i].dwOfs == offset)
539 static void joy_polldev(JoystickImpl *This) {
543 TRACE("(%p)\n", This);
545 if (This->joyfd==-1) {
550 memset(&tv,0,sizeof(tv));
551 FD_ZERO(&readfds);FD_SET(This->joyfd,&readfds);
552 if (1>select(This->joyfd+1,&readfds,NULL,NULL,&tv))
554 /* we have one event, so we can read */
555 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
558 TRACE("js_event: type 0x%x, number %d, value %d\n",
559 jse.type,jse.number,jse.value);
560 if (jse.type & JS_EVENT_BUTTON) {
561 int offset = This->offsets[jse.number + 12];
562 int value = jse.value?0x80:0x00;
564 This->js.rgbButtons[jse.number] = value;
565 GEN_EVENT(offset,value,jse.time,(This->dinput->evsequence)++);
566 } else if (jse.type & JS_EVENT_AXIS) {
567 if (jse.number < 8) {
568 int offset = This->offsets[jse.number];
569 int index = offset_to_object(This, offset);
570 LONG value = map_axis(This, jse.value, index);
572 /* FIXME do deadzone and saturation here */
574 switch (jse.number) {
585 This->js.lRx = value;
588 This->js.lRy = value;
591 This->js.lRz = value;
594 This->js.rglSlider[0] = value;
597 This->js.rglSlider[1] = value;
601 GEN_EVENT(offset,value,jse.time,(This->dinput->evsequence)++);
603 WARN("axis %d not supported\n", jse.number);
608 /******************************************************************************
609 * GetDeviceState : returns the "state" of the joystick.
612 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
613 LPDIRECTINPUTDEVICE8A iface,
617 ICOM_THIS(JoystickImpl,iface);
619 TRACE("(%p,0x%08lx,%p)\n",This,len,ptr);
621 if (!This->acquired) {
622 WARN("not acquired\n");
623 return DIERR_NOTACQUIRED;
626 /* update joystick state */
629 /* convert and copy data to user supplied buffer */
630 fill_DataFormat(ptr, &This->js, This->transform);
635 /******************************************************************************
636 * GetDeviceData : gets buffered input data.
638 static HRESULT WINAPI JoystickAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
640 LPDIDEVICEOBJECTDATA dod,
644 ICOM_THIS(JoystickImpl,iface);
646 FIXME("(%p)->(dods=%ld,entries=%ld,fl=0x%08lx),STUB!\n",This,dodsize,*entries,flags);
649 if (flags & DIGDD_PEEK)
650 FIXME("DIGDD_PEEK\n");
658 int find_property(JoystickImpl * This, LPCDIPROPHEADER ph)
661 if (ph->dwHow == DIPH_BYOFFSET) {
662 return offset_to_object(This, ph->dwObj);
663 } else if (ph->dwHow == DIPH_BYID) {
666 for (i = 0; i < This->user_df->dwNumObjs; i++) {
668 if (DIDFT_GETTYPE(This->user_df->rgodf[i].dwType) & DIDFT_AXIS) {
670 type = DIDFT_GETTYPE(This->user_df->rgodf[i].dwType) |
671 DIDFT_MAKEINSTANCE(axis << WINE_JOYSTICK_AXIS_BASE);
672 TRACE("type = 0x%08lx\n", type);
674 if (DIDFT_GETTYPE(This->user_df->rgodf[i].dwType) & DIDFT_BUTTON) {
676 type = DIDFT_GETTYPE(This->user_df->rgodf[i].dwType) |
677 DIDFT_MAKEINSTANCE(button << WINE_JOYSTICK_BUTTON_BASE);
678 TRACE("type = 0x%08lx\n", type);
680 if (type == ph->dwObj) {
689 /******************************************************************************
690 * SetProperty : change input device properties
692 static HRESULT WINAPI JoystickAImpl_SetProperty(
693 LPDIRECTINPUTDEVICE8A iface,
697 ICOM_THIS(JoystickImpl,iface);
699 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
701 if (TRACE_ON(dinput))
702 _dump_DIPROPHEADER(ph);
704 if (!HIWORD(rguid)) {
705 switch ((DWORD)rguid) {
706 case (DWORD) DIPROP_BUFFERSIZE: {
707 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
708 FIXME("buffersize = %ld\n",pd->dwData);
711 case (DWORD)DIPROP_RANGE: {
712 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
713 int obj = find_property(This, ph);
714 TRACE("proprange(%ld,%ld) obj=%d\n",pr->lMin,pr->lMax,obj);
716 This->props[obj].lMin = pr->lMin;
717 This->props[obj].lMax = pr->lMax;
722 case (DWORD)DIPROP_DEADZONE: {
723 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
724 int obj = find_property(This, ph);
725 TRACE("deadzone(%ld) obj=%d\n",pd->dwData,obj);
727 This->props[obj].lDeadZone = pd->dwData;
732 case (DWORD)DIPROP_SATURATION: {
733 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
734 int obj = find_property(This, ph);
735 TRACE("saturation(%ld) obj=%d\n",pd->dwData,obj);
737 This->props[obj].lSaturation = pd->dwData;
743 FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
751 /******************************************************************************
752 * SetEventNotification : specifies event to be sent on state change
754 static HRESULT WINAPI JoystickAImpl_SetEventNotification(
755 LPDIRECTINPUTDEVICE8A iface, HANDLE hnd
757 ICOM_THIS(JoystickImpl,iface);
759 TRACE("(this=%p,0x%08lx)\n",This,(DWORD)hnd);
764 static void _dump_DIDEVCAPS(LPDIDEVCAPS lpDIDevCaps)
766 TRACE("dwSize: %ld\n", lpDIDevCaps->dwSize);
767 TRACE("dwFlags: %08lx\n",lpDIDevCaps->dwFlags);
768 TRACE("dwDevType: %08lx %s\n", lpDIDevCaps->dwDevType,
769 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
770 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
771 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
772 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
773 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
774 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
775 TRACE("dwAxes: %ld\n",lpDIDevCaps->dwAxes);
776 TRACE("dwButtons: %ld\n",lpDIDevCaps->dwButtons);
777 TRACE("dwPOVs: %ld\n",lpDIDevCaps->dwPOVs);
778 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
779 TRACE("dwFFSamplePeriod: %ld\n",lpDIDevCaps->dwFFSamplePeriod);
780 TRACE("dwFFMinTimeResolution: %ld\n",lpDIDevCaps->dwFFMinTimeResolution);
781 TRACE("dwFirmwareRevision: %ld\n",lpDIDevCaps->dwFirmwareRevision);
782 TRACE("dwHardwareRevision: %ld\n",lpDIDevCaps->dwHardwareRevision);
783 TRACE("dwFFDriverVersion: %ld\n",lpDIDevCaps->dwFFDriverVersion);
787 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
788 LPDIRECTINPUTDEVICE8A iface,
789 LPDIDEVCAPS lpDIDevCaps)
791 ICOM_THIS(JoystickImpl,iface);
793 int xfd = This->joyfd;
795 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
798 xfd = open(JOYDEV,O_RDONLY);
799 lpDIDevCaps->dwFlags = DIDC_ATTACHED;
800 lpDIDevCaps->dwDevType = DIDEVTYPE_JOYSTICK;
802 if (-1==ioctl(xfd,JSIOCGAXES,&axes))
804 lpDIDevCaps->dwAxes = axes;
807 if (-1==ioctl(xfd,JSIOCGAXES,&buttons))
809 lpDIDevCaps->dwButtons = buttons;
811 if (xfd!=This->joyfd)
813 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
814 lpDIDevCaps->dwFFSamplePeriod = 0;
815 lpDIDevCaps->dwFFMinTimeResolution = 0;
816 lpDIDevCaps->dwFirmwareRevision = 0;
817 lpDIDevCaps->dwHardwareRevision = 0;
818 lpDIDevCaps->dwFFDriverVersion = 0;
821 if (TRACE_ON(dinput))
822 _dump_DIDEVCAPS(lpDIDevCaps);
827 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
829 ICOM_THIS(JoystickImpl,iface);
831 TRACE("(%p)\n",This);
833 if (!This->acquired) {
834 WARN("not acquired\n");
835 return DIERR_NOTACQUIRED;
842 /******************************************************************************
843 * EnumObjects : enumerate the different buttons and axis...
845 static HRESULT WINAPI JoystickAImpl_EnumObjects(
846 LPDIRECTINPUTDEVICE8A iface,
847 LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
851 ICOM_THIS(JoystickImpl,iface);
852 DIDEVICEOBJECTINSTANCEA ddoi;
853 int xfd = This->joyfd;
855 TRACE("(this=%p,%p,%p,%08lx)\n", This, lpCallback, lpvRef, dwFlags);
856 if (TRACE_ON(dinput)) {
857 TRACE(" - flags = ");
858 _dump_EnumObjects_flags(dwFlags);
862 /* Only the fields till dwFFMaxForce are relevant */
863 ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
865 #if defined(JSIOCGNAME)
868 if (-1==ioctl(xfd,JSIOCGNAME(sizeof(name)),name)) {
869 This->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
870 strcpy(This->name, name);
871 strcpy(ddoi.tszName, This->name);
874 strcpy(ddoi.tszName, This->name);
877 /* For the joystick, do as is done in the GetCapabilities function */
878 if ((dwFlags == DIDFT_ALL) ||
879 (dwFlags & DIDFT_AXIS)) {
883 if (-1==ioctl(xfd,JSIOCGAXES,&axes))
887 for (i = 0; i < axes; i++) {
890 ddoi.guidType = GUID_XAxis;
891 ddoi.dwOfs = DIJOFS_X;
894 ddoi.guidType = GUID_YAxis;
895 ddoi.dwOfs = DIJOFS_Y;
898 ddoi.guidType = GUID_ZAxis;
899 ddoi.dwOfs = DIJOFS_Z;
902 ddoi.guidType = GUID_RxAxis;
903 ddoi.dwOfs = DIJOFS_RX;
906 ddoi.guidType = GUID_RyAxis;
907 ddoi.dwOfs = DIJOFS_RY;
910 ddoi.guidType = GUID_RzAxis;
911 ddoi.dwOfs = DIJOFS_RZ;
914 ddoi.guidType = GUID_Slider;
915 ddoi.dwOfs = DIJOFS_SLIDER(0);
918 ddoi.guidType = GUID_Slider;
919 ddoi.dwOfs = DIJOFS_SLIDER(1);
922 ddoi.guidType = GUID_Unknown;
923 ddoi.dwOfs = DIJOFS_Z + (i - 2) * sizeof(LONG);
925 ddoi.dwType = DIDFT_MAKEINSTANCE((i + 1) << WINE_JOYSTICK_AXIS_BASE) | DIDFT_AXIS;
926 sprintf(ddoi.tszName, "%d-Axis", i);
927 _dump_OBJECTINSTANCEA(&ddoi);
928 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
932 if ((dwFlags == DIDFT_ALL) ||
933 (dwFlags & DIDFT_BUTTON)) {
937 if (-1==ioctl(xfd,JSIOCGAXES,&buttons))
941 /* The DInput SDK says that GUID_Button is only for mouse buttons but well... */
942 ddoi.guidType = GUID_Button;
944 for (i = 0; i < buttons; i++) {
945 ddoi.dwOfs = DIJOFS_BUTTON(i);
946 ddoi.dwType = DIDFT_MAKEINSTANCE((i + 1) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_BUTTON;
947 sprintf(ddoi.tszName, "%d-Button", i);
948 _dump_OBJECTINSTANCEA(&ddoi);
949 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
953 if (xfd!=This->joyfd)
959 /******************************************************************************
960 * EnumObjects : enumerate the different buttons and axis...
962 static HRESULT WINAPI JoystickWImpl_EnumObjects(
963 LPDIRECTINPUTDEVICE8W iface,
964 LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback,
968 ICOM_THIS(JoystickImpl,iface);
970 device_enumobjects_AtoWcb_data data;
972 data.lpCallBack = lpCallback;
973 data.lpvRef = lpvRef;
975 return JoystickAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
978 /******************************************************************************
979 * GetProperty : get input device properties
981 static HRESULT WINAPI JoystickAImpl_GetProperty(
982 LPDIRECTINPUTDEVICE8A iface,
984 LPDIPROPHEADER pdiph)
986 ICOM_THIS(JoystickImpl,iface);
988 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
990 if (TRACE_ON(dinput))
991 _dump_DIPROPHEADER(pdiph);
993 if (!HIWORD(rguid)) {
994 switch ((DWORD)rguid) {
995 case (DWORD) DIPROP_BUFFERSIZE: {
996 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
997 TRACE(" return buffersize = %d\n",This->queue_len);
998 pd->dwData = This->queue_len;
1001 case (DWORD) DIPROP_RANGE: {
1002 LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
1003 int obj = find_property(This, pdiph);
1004 /* The app is querying the current range of the axis
1005 * return the lMin and lMax values */
1007 pr->lMin = This->props[obj].lMin;
1008 pr->lMax = This->props[obj].lMax;
1009 TRACE("range(%ld, %ld) obj=%d\n", pr->lMin, pr->lMax, obj);
1014 case (DWORD) DIPROP_DEADZONE: {
1015 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1016 int obj = find_property(This, pdiph);
1018 pd->dwData = This->props[obj].lDeadZone;
1019 TRACE("deadzone(%ld) obj=%d\n", pd->dwData, obj);
1024 case (DWORD) DIPROP_SATURATION: {
1025 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1026 int obj = find_property(This, pdiph);
1028 pd->dwData = This->props[obj].lSaturation;
1029 TRACE("saturation(%ld) obj=%d\n", pd->dwData, obj);
1035 FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
1043 /******************************************************************************
1044 * GetObjectInfo : get object info
1046 HRESULT WINAPI JoystickAImpl_GetObjectInfo(
1047 LPDIRECTINPUTDEVICE8A iface,
1048 LPDIDEVICEOBJECTINSTANCEA pdidoi,
1052 ICOM_THIS(JoystickImpl,iface);
1053 DIDEVICEOBJECTINSTANCEA didoiA;
1056 TRACE("(%p,%p,%ld,0x%08lx(%s))\n",
1057 iface, pdidoi, dwObj, dwHow,
1058 dwHow == DIPH_BYOFFSET ? "DIPH_BYOFFSET" :
1059 dwHow == DIPH_BYID ? "DIPH_BYID" :
1060 dwHow == DIPH_BYUSAGE ? "DIPH_BYUSAGE" :
1063 if (pdidoi == NULL) {
1064 WARN("invalid parameter: pdidoi = NULL\n");
1065 return DIERR_INVALIDPARAM;
1068 if ((pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCEA)) &&
1069 (pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3A))) {
1070 WARN("invalid parameter: pdidoi->dwSize = %ld != %d or %d\n",
1071 pdidoi->dwSize, sizeof(DIDEVICEOBJECTINSTANCEA),
1072 sizeof(DIDEVICEOBJECTINSTANCE_DX3A));
1073 return DIERR_INVALIDPARAM;
1076 ZeroMemory(&didoiA, sizeof(didoiA));
1077 didoiA.dwSize = pdidoi->dwSize;
1081 for (i = 0; i < This->user_df->dwNumObjs; i++) {
1082 if (This->user_df->rgodf[i].dwOfs == dwObj) {
1083 if (This->user_df->rgodf[i].pguid)
1084 didoiA.guidType = *This->user_df->rgodf[i].pguid;
1086 didoiA.guidType = GUID_NULL;
1087 didoiA.dwOfs = dwObj;
1088 didoiA.dwType = This->user_df->rgodf[i].dwType;
1089 didoiA.dwFlags = This->user_df->rgodf[i].dwFlags;
1090 strcpy(didoiA.tszName, "?????");
1091 CopyMemory(pdidoi, &didoiA, pdidoi->dwSize);
1097 FIXME("dwHow = DIPH_BYID not implemented\n");
1100 FIXME("dwHow = DIPH_BYUSAGE not implemented\n");
1103 WARN("invalid parameter: dwHow = %08lx\n", dwHow);
1104 return DIERR_INVALIDPARAM;
1107 CopyMemory(pdidoi, &didoiA, pdidoi->dwSize);
1113 static IDirectInputDevice8AVtbl JoystickAvt =
1115 IDirectInputDevice2AImpl_QueryInterface,
1116 IDirectInputDevice2AImpl_AddRef,
1117 JoystickAImpl_Release,
1118 JoystickAImpl_GetCapabilities,
1119 JoystickAImpl_EnumObjects,
1120 JoystickAImpl_GetProperty,
1121 JoystickAImpl_SetProperty,
1122 JoystickAImpl_Acquire,
1123 JoystickAImpl_Unacquire,
1124 JoystickAImpl_GetDeviceState,
1125 JoystickAImpl_GetDeviceData,
1126 JoystickAImpl_SetDataFormat,
1127 JoystickAImpl_SetEventNotification,
1128 IDirectInputDevice2AImpl_SetCooperativeLevel,
1129 JoystickAImpl_GetObjectInfo,
1130 IDirectInputDevice2AImpl_GetDeviceInfo,
1131 IDirectInputDevice2AImpl_RunControlPanel,
1132 IDirectInputDevice2AImpl_Initialize,
1133 IDirectInputDevice2AImpl_CreateEffect,
1134 IDirectInputDevice2AImpl_EnumEffects,
1135 IDirectInputDevice2AImpl_GetEffectInfo,
1136 IDirectInputDevice2AImpl_GetForceFeedbackState,
1137 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1138 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1139 IDirectInputDevice2AImpl_Escape,
1141 IDirectInputDevice2AImpl_SendDeviceData,
1142 IDirectInputDevice7AImpl_EnumEffectsInFile,
1143 IDirectInputDevice7AImpl_WriteEffectToFile,
1144 IDirectInputDevice8AImpl_BuildActionMap,
1145 IDirectInputDevice8AImpl_SetActionMap,
1146 IDirectInputDevice8AImpl_GetImageInfo
1149 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1150 # define XCAST(fun) (typeof(SysJoystickWvt.fun))
1152 # define XCAST(fun) (void*)
1155 static IDirectInputDevice8WVtbl SysJoystickWvt =
1157 IDirectInputDevice2WImpl_QueryInterface,
1158 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1159 XCAST(Release)JoystickAImpl_Release,
1160 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1161 JoystickWImpl_EnumObjects,
1162 XCAST(GetProperty)JoystickAImpl_GetProperty,
1163 XCAST(SetProperty)JoystickAImpl_SetProperty,
1164 XCAST(Acquire)JoystickAImpl_Acquire,
1165 XCAST(Unacquire)JoystickAImpl_Unacquire,
1166 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1167 XCAST(GetDeviceData)JoystickAImpl_GetDeviceData,
1168 XCAST(SetDataFormat)JoystickAImpl_SetDataFormat,
1169 XCAST(SetEventNotification)JoystickAImpl_SetEventNotification,
1170 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1171 IDirectInputDevice2WImpl_GetObjectInfo,
1172 IDirectInputDevice2WImpl_GetDeviceInfo,
1173 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1174 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1175 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1176 IDirectInputDevice2WImpl_EnumEffects,
1177 IDirectInputDevice2WImpl_GetEffectInfo,
1178 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1179 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1180 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1181 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1182 XCAST(Poll)JoystickAImpl_Poll,
1183 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1184 IDirectInputDevice7WImpl_EnumEffectsInFile,
1185 IDirectInputDevice7WImpl_WriteEffectToFile,
1186 IDirectInputDevice8WImpl_BuildActionMap,
1187 IDirectInputDevice8WImpl_SetActionMap,
1188 IDirectInputDevice8WImpl_GetImageInfo
1192 #endif /* HAVE_LINUX_22_JOYSTICK_API */