1 /* DirectInput Joystick device for Mac OS/X
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2009 CodeWeavers, Aric Stewart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
26 #if defined(HAVE_IOKIT_HID_IOHIDLIB_H)
28 #define LPDWORD UInt32*
30 #define LPLONG SInt32*
31 #define E_PENDING __carbon_E_PENDING
32 #define ULONG __carbon_ULONG
33 #define E_INVALIDARG __carbon_E_INVALIDARG
34 #define E_OUTOFMEMORY __carbon_E_OUTOFMEMORY
35 #define E_HANDLE __carbon_E_HANDLE
36 #define E_ACCESSDENIED __carbon_E_ACCESSDENIED
37 #define E_UNEXPECTED __carbon_E_UNEXPECTED
38 #define E_FAIL __carbon_E_FAIL
39 #define E_ABORT __carbon_E_ABORT
40 #define E_POINTER __carbon_E_POINTER
41 #define E_NOINTERFACE __carbon_E_NOINTERFACE
42 #define E_NOTIMPL __carbon_E_NOTIMPL
43 #define S_FALSE __carbon_S_FALSE
44 #define S_OK __carbon_S_OK
45 #define HRESULT_FACILITY __carbon_HRESULT_FACILITY
46 #define IS_ERROR __carbon_IS_ERROR
47 #define FAILED __carbon_FAILED
48 #define SUCCEEDED __carbon_SUCCEEDED
49 #define MAKE_HRESULT __carbon_MAKE_HRESULT
50 #define HRESULT __carbon_HRESULT
51 #define STDMETHODCALLTYPE __carbon_STDMETHODCALLTYPE
52 #include <IOKit/IOKitLib.h>
53 #include <IOKit/hid/IOHIDLib.h>
54 #include <ForceFeedback/ForceFeedback.h>
68 #undef HRESULT_FACILITY
74 #undef STDMETHODCALLTYPE
80 #endif /* HAVE_IOKIT_HID_IOHIDLIB_H */
82 #include "wine/debug.h"
83 #include "wine/unicode.h"
91 #include "dinput_private.h"
92 #include "device_private.h"
93 #include "joystick_private.h"
95 #ifdef HAVE_IOHIDMANAGERCREATE
97 WINE_DEFAULT_DEBUG_CHANNEL(dinput
);
99 #define MAKEUINT64(high, low) (((uint64_t)high << 32) | (uint32_t)low)
101 static CFMutableArrayRef device_main_elements
= NULL
;
103 typedef struct JoystickImpl JoystickImpl
;
104 static const IDirectInputDevice8WVtbl JoystickWvt
;
108 struct JoystickGenericImpl generic
;
114 FFDeviceObjectReference ff
;
118 static inline JoystickImpl
*impl_from_IDirectInputDevice8W(IDirectInputDevice8W
*iface
)
120 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface
, IDirectInputDeviceImpl
, IDirectInputDevice8W_iface
),
121 JoystickGenericImpl
, base
), JoystickImpl
, generic
);
124 typedef struct _EffectImpl
{
125 IDirectInputEffect IDirectInputEffect_iface
;
128 JoystickImpl
*device
;
129 FFEffectObjectReference effect
;
135 static EffectImpl
*impl_from_IDirectInputEffect(IDirectInputEffect
*iface
)
137 return CONTAINING_RECORD(iface
, EffectImpl
, IDirectInputEffect_iface
);
140 static const IDirectInputEffectVtbl EffectVtbl
;
142 static const GUID DInput_Wine_OsX_Joystick_GUID
= { /* 59CAD8F6-E617-41E2-8EB7-47B23EEEDC5A */
143 0x59CAD8F6, 0xE617, 0x41E2, {0x8E, 0xB7, 0x47, 0xB2, 0x3E, 0xEE, 0xDC, 0x5A}
146 static HRESULT
osx_to_win32_hresult(HRESULT in
)
148 /* OSX returns 16-bit COM runtime errors, which we should
149 * convert to win32 */
154 return E_OUTOFMEMORY
;
158 return E_NOINTERFACE
;
168 return E_ACCESSDENIED
;
175 static long get_device_property_long(IOHIDDeviceRef device
, CFStringRef key
)
182 assert(IOHIDDeviceGetTypeID() == CFGetTypeID(device
));
184 ref
= IOHIDDeviceGetProperty(device
, key
);
186 if (ref
&& CFNumberGetTypeID() == CFGetTypeID(ref
))
187 CFNumberGetValue((CFNumberRef
)ref
, kCFNumberLongType
, &result
);
193 static CFStringRef
copy_device_name(IOHIDDeviceRef device
)
201 assert(IOHIDDeviceGetTypeID() == CFGetTypeID(device
));
203 ref_name
= IOHIDDeviceGetProperty(device
, CFSTR(kIOHIDProductKey
));
205 if (ref_name
&& CFStringGetTypeID() == CFGetTypeID(ref_name
))
206 name
= CFStringCreateCopy(kCFAllocatorDefault
, ref_name
);
211 vendID
= get_device_property_long(device
, CFSTR(kIOHIDVendorIDKey
));
212 prodID
= get_device_property_long(device
, CFSTR(kIOHIDProductIDKey
));
213 name
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("0x%04lx 0x%04lx"), vendID
, prodID
);
218 ERR("NULL device\n");
219 name
= CFStringCreateCopy(kCFAllocatorDefault
, CFSTR(""));
225 static long get_device_location_ID(IOHIDDeviceRef device
)
227 return get_device_property_long(device
, CFSTR(kIOHIDLocationIDKey
));
230 static void copy_set_to_array(const void *value
, void *context
)
232 CFArrayAppendValue(context
, value
);
235 static CFComparisonResult
device_name_comparator(IOHIDDeviceRef device1
, IOHIDDeviceRef device2
)
237 CFStringRef name1
= copy_device_name(device1
), name2
= copy_device_name(device2
);
238 CFComparisonResult result
= CFStringCompare(name1
, name2
, (kCFCompareForcedOrdering
| kCFCompareNumerically
));
244 static CFComparisonResult
device_location_name_comparator(const void *val1
, const void *val2
, void *context
)
246 IOHIDDeviceRef device1
= (IOHIDDeviceRef
)val1
, device2
= (IOHIDDeviceRef
)val2
;
247 long loc1
= get_device_location_ID(device1
), loc2
= get_device_location_ID(device2
);
250 return kCFCompareLessThan
;
251 else if (loc1
> loc2
)
252 return kCFCompareGreaterThan
;
253 /* virtual joysticks may not have a kIOHIDLocationIDKey and will default to location ID of 0, this orders virtual joysticks by their name */
254 return device_name_comparator(device1
, device2
);
257 static const char* debugstr_cf(CFTypeRef t
)
262 if (!t
) return "(null)";
264 if (CFGetTypeID(t
) == CFStringGetTypeID())
267 s
= CFCopyDescription(t
);
268 ret
= CFStringGetCStringPtr(s
, kCFStringEncodingUTF8
);
269 if (ret
) ret
= debugstr_a(ret
);
272 const UniChar
* u
= CFStringGetCharactersPtr(s
);
274 ret
= debugstr_wn((const WCHAR
*)u
, CFStringGetLength(s
));
279 int len
= min(CFStringGetLength(s
), ARRAY_SIZE(buf
));
280 CFStringGetCharacters(s
, CFRangeMake(0, len
), buf
);
281 ret
= debugstr_wn(buf
, len
);
283 if (s
!= t
) CFRelease(s
);
287 static const char* debugstr_device(IOHIDDeviceRef device
)
289 return wine_dbg_sprintf("<IOHIDDevice %p product %s IOHIDLocationID %lu>", device
,
290 debugstr_cf(IOHIDDeviceGetProperty(device
, CFSTR(kIOHIDProductKey
))),
291 get_device_location_ID(device
));
294 static const char* debugstr_element(IOHIDElementRef element
)
296 return wine_dbg_sprintf("<IOHIDElement %p type %d usage %u/%u device %p>", element
,
297 IOHIDElementGetType(element
), IOHIDElementGetUsagePage(element
),
298 IOHIDElementGetUsage(element
), IOHIDElementGetDevice(element
));
301 static IOHIDDeviceRef
get_device_ref(int id
)
303 IOHIDElementRef device_main_element
;
304 IOHIDDeviceRef hid_device
;
306 TRACE("id %d\n", id
);
308 if (!device_main_elements
|| id
>= CFArrayGetCount(device_main_elements
))
311 device_main_element
= (IOHIDElementRef
)CFArrayGetValueAtIndex(device_main_elements
, id
);
312 if (!device_main_element
)
314 ERR("Invalid Element requested %i\n",id
);
318 hid_device
= IOHIDElementGetDevice(device_main_element
);
321 ERR("Invalid Device requested %i\n",id
);
325 TRACE("-> %s\n", debugstr_device(hid_device
));
329 static HRESULT
get_ff(IOHIDDeviceRef device
, FFDeviceObjectReference
*ret
)
331 io_service_t service
;
332 CFMutableDictionaryRef matching
;
333 CFTypeRef location_id
;
336 TRACE("device %s\n", debugstr_device(device
));
338 matching
= IOServiceMatching(kIOHIDDeviceKey
);
340 WARN("IOServiceMatching failed, force feedback disabled\n");
341 return DIERR_DEVICENOTREG
;
344 location_id
= IOHIDDeviceGetProperty(device
, CFSTR(kIOHIDLocationIDKey
));
347 WARN("IOHIDDeviceGetProperty failed, force feedback disabled\n");
348 return DIERR_DEVICENOTREG
;
351 CFDictionaryAddValue(matching
, CFSTR(kIOHIDLocationIDKey
), location_id
);
353 service
= IOServiceGetMatchingService(kIOMasterPortDefault
, matching
);
356 hr
= osx_to_win32_hresult(FFCreateDevice(service
, ret
));
358 hr
= FFIsForceFeedback(service
) == FF_OK
? S_OK
: S_FALSE
;
360 IOObjectRelease(service
);
361 TRACE("-> hr 0x%08x *ret %p\n", hr
, ret
? *ret
: NULL
);
365 static CFMutableDictionaryRef
create_osx_device_match(int usage
)
367 CFMutableDictionaryRef result
;
369 TRACE("usage %d\n", usage
);
371 result
= CFDictionaryCreateMutable( kCFAllocatorDefault
, 0,
372 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
376 int number
= kHIDPage_GenericDesktop
;
377 CFNumberRef page
= CFNumberCreate( kCFAllocatorDefault
,
378 kCFNumberIntType
, &number
);
382 CFNumberRef cf_usage
;
384 CFDictionarySetValue( result
, CFSTR( kIOHIDDeviceUsagePageKey
), page
);
387 cf_usage
= CFNumberCreate( kCFAllocatorDefault
,
388 kCFNumberIntType
, &usage
);
391 CFDictionarySetValue( result
, CFSTR( kIOHIDDeviceUsageKey
), cf_usage
);
392 CFRelease( cf_usage
);
396 ERR("CFNumberCreate() failed.\n");
403 ERR("CFNumberCreate failed.\n");
410 ERR("CFDictionaryCreateMutable failed.\n");
417 static CFIndex
find_top_level(IOHIDDeviceRef hid_device
, CFMutableArrayRef main_elements
)
422 TRACE("hid_device %s\n", debugstr_device(hid_device
));
427 elements
= IOHIDDeviceCopyMatchingElements(hid_device
, NULL
, 0);
431 CFIndex idx
, cnt
= CFArrayGetCount(elements
);
432 for (idx
=0; idx
<cnt
; idx
++)
434 IOHIDElementRef element
= (IOHIDElementRef
)CFArrayGetValueAtIndex(elements
, idx
);
435 int type
= IOHIDElementGetType(element
);
437 TRACE("element %s\n", debugstr_element(element
));
439 /* Check for top-level gaming device collections */
440 if (type
== kIOHIDElementTypeCollection
&& IOHIDElementGetParent(element
) == 0)
442 int usage_page
= IOHIDElementGetUsagePage(element
);
443 int usage
= IOHIDElementGetUsage(element
);
445 if (usage_page
== kHIDPage_GenericDesktop
&&
446 (usage
== kHIDUsage_GD_Joystick
|| usage
== kHIDUsage_GD_GamePad
))
448 CFArrayAppendValue(main_elements
, element
);
456 TRACE("-> total %d\n", (int)total
);
460 static void get_element_children(IOHIDElementRef element
, CFMutableArrayRef all_children
)
463 CFArrayRef element_children
= IOHIDElementGetChildren(element
);
465 TRACE("element %s\n", debugstr_element(element
));
467 cnt
= CFArrayGetCount(element_children
);
469 /* Either add the element to the array or grab its children */
470 for (idx
=0; idx
<cnt
; idx
++)
472 IOHIDElementRef child
;
474 child
= (IOHIDElementRef
)CFArrayGetValueAtIndex(element_children
, idx
);
475 TRACE("child %s\n", debugstr_element(child
));
476 if (IOHIDElementGetType(child
) == kIOHIDElementTypeCollection
)
477 get_element_children(child
, all_children
);
479 CFArrayAppendValue(all_children
, child
);
483 static int find_osx_devices(void)
485 IOHIDManagerRef hid_manager
;
486 CFMutableDictionaryRef result
;
488 CFMutableArrayRef matching
;
492 hid_manager
= IOHIDManagerCreate( kCFAllocatorDefault
, 0L );
493 if (IOHIDManagerOpen( hid_manager
, 0 ) != kIOReturnSuccess
)
495 ERR("Couldn't open IOHIDManager.\n");
496 CFRelease( hid_manager
);
500 matching
= CFArrayCreateMutable( kCFAllocatorDefault
, 0,
501 &kCFTypeArrayCallBacks
);
503 /* build matching dictionary */
504 result
= create_osx_device_match(kHIDUsage_GD_Joystick
);
510 CFArrayAppendValue( matching
, result
);
512 result
= create_osx_device_match(kHIDUsage_GD_GamePad
);
518 CFArrayAppendValue( matching
, result
);
521 IOHIDManagerSetDeviceMatchingMultiple( hid_manager
, matching
);
522 CFRelease( matching
);
523 devset
= IOHIDManagerCopyDevices( hid_manager
);
526 CFIndex num_devices
, num_main_elements
, idx
;
527 CFMutableArrayRef devices
;
529 num_devices
= CFSetGetCount(devset
);
530 devices
= CFArrayCreateMutable(kCFAllocatorDefault
, num_devices
, &kCFTypeArrayCallBacks
);
531 CFSetApplyFunction(devset
, copy_set_to_array
, devices
);
533 CFArraySortValues(devices
, CFRangeMake(0, num_devices
), device_location_name_comparator
, NULL
);
535 device_main_elements
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeArrayCallBacks
);
536 if (!device_main_elements
)
538 CFRelease( devices
);
542 num_main_elements
= 0;
543 for (idx
= 0; idx
< num_devices
; idx
++)
546 IOHIDDeviceRef hid_device
;
548 hid_device
= (IOHIDDeviceRef
) CFArrayGetValueAtIndex(devices
, idx
);
549 TRACE("hid_device %s\n", debugstr_device(hid_device
));
550 top
= find_top_level(hid_device
, device_main_elements
);
551 num_main_elements
+= top
;
556 TRACE("found %i device(s), %i collection(s)\n",(int)num_devices
,(int)num_main_elements
);
557 return (int)num_main_elements
;
561 IOHIDManagerClose( hid_manager
, 0 );
562 CFRelease( hid_manager
);
566 static int get_osx_device_name(int id
, char *name
, int length
)
569 IOHIDDeviceRef hid_device
;
571 hid_device
= get_device_ref(id
);
573 TRACE("id %d hid_device %s\n", id
, debugstr_device(hid_device
));
581 str
= IOHIDDeviceGetProperty(hid_device
, CFSTR( kIOHIDProductKey
));
584 CFIndex len
= CFStringGetLength(str
);
587 CFStringGetCString(str
,name
,length
,kCFStringEncodingASCII
);
596 static CFComparisonResult
button_usage_comparator(const void *val1
, const void *val2
, void *context
)
598 IOHIDElementRef element1
= (IOHIDElementRef
)val1
, element2
= (IOHIDElementRef
)val2
;
599 int usage1
= IOHIDElementGetUsage(element1
), usage2
= IOHIDElementGetUsage(element2
);
602 return kCFCompareLessThan
;
604 return kCFCompareGreaterThan
;
605 return kCFCompareEqualTo
;
608 static void get_osx_device_elements(JoystickImpl
*device
, uint64_t axis_map
[8])
610 IOHIDElementRef device_main_element
;
611 CFMutableArrayRef elements
;
613 BOOL use_accel_brake_for_rx_ry
= TRUE
;
615 TRACE("device %p device->id %d\n", device
, device
->id
);
617 device
->elements
= NULL
;
619 if (!device_main_elements
|| device
->id
>= CFArrayGetCount(device_main_elements
))
622 device_main_element
= (IOHIDElementRef
)CFArrayGetValueAtIndex(device_main_elements
, device
->id
);
623 TRACE("device_main_element %s\n", debugstr_element(device_main_element
));
624 if (!device_main_element
)
627 elements
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeArrayCallBacks
);
628 get_element_children(device_main_element
, elements
);
632 CFIndex idx
, cnt
= CFArrayGetCount( elements
);
633 CFMutableArrayRef axes
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeArrayCallBacks
);
634 CFMutableArrayRef buttons
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeArrayCallBacks
);
635 CFMutableArrayRef povs
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeArrayCallBacks
);
637 /* Scan the elements to see if Rx/Ry is present, if not then Accelerator/Brake can be mapped to it.
638 * (Xbox One controller triggers use accelerator/brake)
640 for ( idx
= 0; idx
< cnt
; idx
++ )
642 IOHIDElementRef element
= ( IOHIDElementRef
) CFArrayGetValueAtIndex( elements
, idx
);
643 IOHIDElementType type
= IOHIDElementGetType( element
);
644 uint32_t usage_page
= IOHIDElementGetUsagePage( element
);
645 uint32_t usage
= IOHIDElementGetUsage( element
);
647 if (type
== kIOHIDElementTypeInput_Misc
&& usage_page
== kHIDPage_GenericDesktop
&&
648 (usage
== kHIDUsage_GD_Rx
|| usage
== kHIDUsage_GD_Ry
))
650 use_accel_brake_for_rx_ry
= FALSE
;
654 for ( idx
= 0; idx
< cnt
; idx
++ )
656 IOHIDElementRef element
= ( IOHIDElementRef
) CFArrayGetValueAtIndex( elements
, idx
);
657 int type
= IOHIDElementGetType( element
);
658 int usage_page
= IOHIDElementGetUsagePage( element
);
660 TRACE("element %s\n", debugstr_element(element
));
662 if (usage_page
>= kHIDPage_VendorDefinedStart
)
664 /* vendor pages can repurpose type ids, resulting in incorrect case matches below (e.g. ds4 controllers) */
670 case kIOHIDElementTypeInput_Button
:
672 TRACE("kIOHIDElementTypeInput_Button usage_page %d\n", usage_page
);
673 if ((usage_page
!= kHIDPage_Button
) && (usage_page
!= kHIDPage_Consumer
))
675 /* avoid strange elements found on the 360 controller */
679 if (CFArrayGetCount(buttons
) < 128)
680 CFArrayAppendValue(buttons
, element
);
683 case kIOHIDElementTypeInput_Axis
:
685 TRACE("kIOHIDElementTypeInput_Axis\n");
686 CFArrayAppendValue(axes
, element
);
689 case kIOHIDElementTypeInput_Misc
:
691 uint32_t usage
= IOHIDElementGetUsage( element
);
692 switch(MAKEUINT64(usage_page
, usage
))
694 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Hatswitch
):
696 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Hatswitch\n");
697 CFArrayAppendValue(povs
, element
);
700 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Slider
):
704 /* fallthrough, sliders are axis */
705 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_X
):
706 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Y
):
707 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Z
):
708 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Rx
):
709 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Ry
):
710 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Rz
):
711 case MAKEUINT64(kHIDPage_Simulation
, kHIDUsage_Sim_Accelerator
):
712 case MAKEUINT64(kHIDPage_Simulation
, kHIDUsage_Sim_Brake
):
714 if (usage
== kHIDUsage_Sim_Accelerator
|| usage
== kHIDUsage_Sim_Brake
)
716 if (use_accel_brake_for_rx_ry
)
717 TRACE("Using Sim_Accelerator/Brake for GD_Rx/Ry\n");
722 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_* (%d)\n", usage
);
723 axis_map
[CFArrayGetCount(axes
)]=MAKEUINT64(usage_page
, usage
);
724 CFArrayAppendValue(axes
, element
);
728 FIXME("kIOHIDElementTypeInput_Misc / Unhandled usage %i/%i\n", usage_page
, usage
);
733 FIXME("Unhandled type %i\n",type
);
737 /* Sort buttons into correct order */
738 CFArraySortValues(buttons
, CFRangeMake(0, CFArrayGetCount(buttons
)), button_usage_comparator
, NULL
);
740 device
->generic
.devcaps
.dwAxes
= CFArrayGetCount(axes
);
741 device
->generic
.devcaps
.dwButtons
= CFArrayGetCount(buttons
);
742 device
->generic
.devcaps
.dwPOVs
= CFArrayGetCount(povs
);
744 TRACE("axes %u povs %u buttons %u\n", device
->generic
.devcaps
.dwAxes
, device
->generic
.devcaps
.dwPOVs
,
745 device
->generic
.devcaps
.dwButtons
);
747 /* build our element array in the order that dinput expects */
748 CFArrayAppendArray(axes
, povs
, CFRangeMake(0, device
->generic
.devcaps
.dwPOVs
));
749 CFArrayAppendArray(axes
, buttons
, CFRangeMake(0, device
->generic
.devcaps
.dwButtons
));
750 device
->elements
= axes
;
759 device
->generic
.devcaps
.dwAxes
= 0;
760 device
->generic
.devcaps
.dwButtons
= 0;
761 device
->generic
.devcaps
.dwPOVs
= 0;
765 static void get_osx_device_elements_props(JoystickImpl
*device
)
767 TRACE("device %p\n", device
);
769 if (device
->elements
)
771 CFIndex idx
, cnt
= CFArrayGetCount( device
->elements
);
773 for ( idx
= 0; idx
< cnt
; idx
++ )
775 IOHIDElementRef element
= ( IOHIDElementRef
) CFArrayGetValueAtIndex( device
->elements
, idx
);
777 TRACE("element %s\n", debugstr_element(element
));
779 device
->generic
.props
[idx
].lDevMin
= IOHIDElementGetLogicalMin(element
);
780 device
->generic
.props
[idx
].lDevMax
= IOHIDElementGetLogicalMax(element
);
781 device
->generic
.props
[idx
].lMin
= 0;
782 device
->generic
.props
[idx
].lMax
= 0xffff;
783 device
->generic
.props
[idx
].lDeadZone
= 0;
784 device
->generic
.props
[idx
].lSaturation
= 0;
789 static void poll_osx_device_state( IDirectInputDevice8W
*iface
)
791 JoystickImpl
*device
= impl_from_IDirectInputDevice8W( iface
);
792 IOHIDElementRef device_main_element
;
793 IOHIDDeviceRef hid_device
;
795 TRACE("device %p device->id %i\n", device
, device
->id
);
797 if (!device_main_elements
|| device
->id
>= CFArrayGetCount(device_main_elements
))
800 device_main_element
= (IOHIDElementRef
) CFArrayGetValueAtIndex(device_main_elements
, device
->id
);
801 hid_device
= IOHIDElementGetDevice(device_main_element
);
802 TRACE("main element %s hid_device %s\n", debugstr_element(device_main_element
), debugstr_device(hid_device
));
806 if (device
->elements
)
812 CFIndex idx
, cnt
= CFArrayGetCount( device
->elements
);
814 for ( idx
= 0; idx
< cnt
; idx
++ )
816 IOHIDValueRef valueRef
;
817 int val
, oldVal
, newVal
;
818 IOHIDElementRef element
= ( IOHIDElementRef
) CFArrayGetValueAtIndex( device
->elements
, idx
);
819 int type
= IOHIDElementGetType( element
);
821 TRACE("element %s\n", debugstr_element(element
));
825 case kIOHIDElementTypeInput_Button
:
826 TRACE("kIOHIDElementTypeInput_Button\n");
830 if (IOHIDDeviceGetValue(hid_device
, element
, &valueRef
) != kIOReturnSuccess
)
832 if (valueRef
== NULL
)
834 val
= IOHIDValueGetIntegerValue(valueRef
);
835 newVal
= val
? 0x80 : 0x0;
836 oldVal
= device
->generic
.js
.rgbButtons
[button_idx
];
837 device
->generic
.js
.rgbButtons
[button_idx
] = newVal
;
838 TRACE("valueRef %s val %d oldVal %d newVal %d\n", debugstr_cf(valueRef
), val
, oldVal
, newVal
);
839 if (oldVal
!= newVal
)
841 inst_id
= DIDFT_MAKEINSTANCE(button_idx
) | DIDFT_PSHBUTTON
;
842 queue_event(iface
,inst_id
,newVal
,GetCurrentTime(),device
->generic
.base
.dinput
->evsequence
++);
847 case kIOHIDElementTypeInput_Misc
:
849 uint32_t usage_page
= IOHIDElementGetUsagePage( element
);
850 uint32_t usage
= IOHIDElementGetUsage( element
);
851 switch(MAKEUINT64(usage_page
, usage
))
853 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Hatswitch
):
855 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Hatswitch\n");
857 if (IOHIDDeviceGetValue(hid_device
, element
, &valueRef
) != kIOReturnSuccess
)
859 if (valueRef
== NULL
)
861 val
= IOHIDValueGetIntegerValue(valueRef
);
862 oldVal
= device
->generic
.js
.rgdwPOV
[pov_idx
];
863 if ((val
> device
->generic
.props
[idx
].lDevMax
) || (val
< device
->generic
.props
[idx
].lDevMin
))
866 newVal
= (val
- device
->generic
.props
[idx
].lDevMin
) * 4500;
867 device
->generic
.js
.rgdwPOV
[pov_idx
] = newVal
;
868 TRACE("valueRef %s val %d oldVal %d newVal %d\n", debugstr_cf(valueRef
), val
, oldVal
, newVal
);
869 if (oldVal
!= newVal
)
871 inst_id
= DIDFT_MAKEINSTANCE(pov_idx
) | DIDFT_POV
;
872 queue_event(iface
,inst_id
,newVal
,GetCurrentTime(),device
->generic
.base
.dinput
->evsequence
++);
877 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_X
):
878 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Y
):
879 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Z
):
880 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Rx
):
881 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Ry
):
882 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Rz
):
883 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Slider
):
884 case MAKEUINT64(kHIDPage_Simulation
, kHIDUsage_Sim_Accelerator
):
885 case MAKEUINT64(kHIDPage_Simulation
, kHIDUsage_Sim_Brake
):
890 if (IOHIDDeviceGetValue(hid_device
, element
, &valueRef
) != kIOReturnSuccess
)
892 if (valueRef
== NULL
)
894 val
= IOHIDValueGetIntegerValue(valueRef
);
895 newVal
= joystick_map_axis(&device
->generic
.props
[idx
], val
);
896 switch (MAKEUINT64(usage_page
, usage
))
898 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_X
):
899 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_X\n");
901 oldVal
= device
->generic
.js
.lX
;
902 device
->generic
.js
.lX
= newVal
;
904 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Y
):
905 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Y\n");
907 oldVal
= device
->generic
.js
.lY
;
908 device
->generic
.js
.lY
= newVal
;
910 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Z
):
911 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Z\n");
913 oldVal
= device
->generic
.js
.lZ
;
914 device
->generic
.js
.lZ
= newVal
;
916 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Rx
):
917 case MAKEUINT64(kHIDPage_Simulation
, kHIDUsage_Sim_Accelerator
):
918 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Rx\n");
920 oldVal
= device
->generic
.js
.lRx
;
921 device
->generic
.js
.lRx
= newVal
;
923 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Ry
):
924 case MAKEUINT64(kHIDPage_Simulation
, kHIDUsage_Sim_Brake
):
925 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Ry\n");
927 oldVal
= device
->generic
.js
.lRy
;
928 device
->generic
.js
.lRy
= newVal
;
930 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Rz
):
931 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Rz\n");
933 oldVal
= device
->generic
.js
.lRz
;
934 device
->generic
.js
.lRz
= newVal
;
936 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Slider
):
937 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Slider\n");
938 wine_obj
= 6 + slider_idx
;
939 oldVal
= device
->generic
.js
.rglSlider
[slider_idx
];
940 device
->generic
.js
.rglSlider
[slider_idx
] = newVal
;
944 TRACE("valueRef %s val %d oldVal %d newVal %d\n", debugstr_cf(valueRef
), val
, oldVal
, newVal
);
945 if ((wine_obj
!= -1) &&
948 inst_id
= DIDFT_MAKEINSTANCE(wine_obj
) | DIDFT_ABSAXIS
;
949 queue_event(iface
,inst_id
,newVal
,GetCurrentTime(),device
->generic
.base
.dinput
->evsequence
++);
955 FIXME("kIOHIDElementTypeInput_Misc / unhandled usage %i\n", usage
);
960 FIXME("Unhandled type %i\n",type
);
966 static INT
find_joystick_devices(void)
968 static INT joystick_devices_count
= -1;
970 if (joystick_devices_count
!= -1) return joystick_devices_count
;
972 joystick_devices_count
= find_osx_devices();
974 return joystick_devices_count
;
977 static DWORD
make_vid_pid(IOHIDDeviceRef device
)
981 vendID
= get_device_property_long(device
, CFSTR(kIOHIDVendorIDKey
));
982 prodID
= get_device_property_long(device
, CFSTR(kIOHIDProductIDKey
));
984 return MAKELONG(vendID
, prodID
);
987 static HRESULT
joydev_enum_device(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEW lpddi
, DWORD version
, int id
)
991 IOHIDDeviceRef device
;
994 TRACE("dwDevType %u dwFlags 0x%08x version 0x%04x id %d\n", dwDevType
, dwFlags
, version
, id
);
996 if (id
>= find_joystick_devices()) return E_FAIL
;
998 device
= get_device_ref(id
);
1000 if ((dwDevType
== 0) ||
1001 ((dwDevType
== DIDEVTYPE_JOYSTICK
) && (version
>= 0x0300 && version
< 0x0800)) ||
1002 (((dwDevType
== DI8DEVCLASS_GAMECTRL
) || (dwDevType
== DI8DEVTYPE_JOYSTICK
)) && (version
>= 0x0800))) {
1004 if (dwFlags
& DIEDFL_FORCEFEEDBACK
) {
1007 if(get_ff(device
, NULL
) != S_OK
)
1010 is_joystick
= get_device_property_long(device
, CFSTR(kIOHIDDeviceUsageKey
)) == kHIDUsage_GD_Joystick
;
1011 /* Return joystick */
1012 lpddi
->guidInstance
= DInput_Wine_OsX_Joystick_GUID
;
1013 lpddi
->guidInstance
.Data3
= id
;
1014 lpddi
->guidProduct
= DInput_PIDVID_Product_GUID
;
1015 lpddi
->guidProduct
.Data1
= make_vid_pid(device
);
1016 lpddi
->dwDevType
= get_device_type(version
, is_joystick
);
1017 lpddi
->dwDevType
|= DIDEVTYPE_HID
;
1018 lpddi
->wUsagePage
= 0x01; /* Desktop */
1020 lpddi
->wUsage
= 0x04; /* Joystick */
1022 lpddi
->wUsage
= 0x05; /* Game Pad */
1023 sprintf(friendly
, "Joystick %d", id
);
1024 MultiByteToWideChar(CP_ACP
, 0, friendly
, -1, lpddi
->tszInstanceName
, MAX_PATH
);
1026 /* get the device name */
1027 get_osx_device_name(id
, name
, MAX_PATH
);
1029 MultiByteToWideChar(CP_ACP
, 0, name
, -1, lpddi
->tszProductName
, MAX_PATH
);
1030 lpddi
->guidFFDriver
= GUID_NULL
;
1037 static const char *osx_ff_axis_name(UInt8 axis
)
1048 sprintf(ret
, "%u", (unsigned int)axis
);
1052 static BOOL
osx_axis_has_ff(FFCAPABILITIES
*ffcaps
, UInt8 axis
)
1055 for(i
= 0; i
< ffcaps
->numFfAxes
; ++i
)
1056 if(ffcaps
->ffAxes
[i
] == axis
)
1061 static HRESULT
alloc_device( REFGUID rguid
, IDirectInputImpl
*dinput
, JoystickImpl
**out
, unsigned short index
)
1064 IOHIDDeviceRef device
;
1065 JoystickImpl
* newDevice
;
1066 char name
[MAX_PATH
];
1068 LPDIDATAFORMAT df
= NULL
;
1070 uint64_t axis_map
[8]; /* max axes */
1071 int slider_count
= 0;
1072 FFCAPABILITIES ffcaps
;
1074 TRACE( "%s %p %p %hu\n", debugstr_guid( rguid
), dinput
, out
, index
);
1076 if (FAILED(hr
= direct_input_device_alloc( sizeof(JoystickImpl
), &JoystickWvt
, rguid
, dinput
, (void **)&newDevice
)))
1078 newDevice
->generic
.base
.crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JoystickImpl*->generic.base.crit");
1080 newDevice
->id
= index
;
1082 device
= get_device_ref(index
);
1084 newDevice
->generic
.guidInstance
= DInput_Wine_OsX_Joystick_GUID
;
1085 newDevice
->generic
.guidInstance
.Data3
= index
;
1086 newDevice
->generic
.guidProduct
= DInput_PIDVID_Product_GUID
;
1087 newDevice
->generic
.guidProduct
.Data1
= make_vid_pid(device
);
1088 newDevice
->generic
.joy_polldev
= poll_osx_device_state
;
1090 /* get the device name */
1091 get_osx_device_name(index
, name
, MAX_PATH
);
1092 TRACE("Name %s\n",name
);
1094 /* copy the device name */
1095 newDevice
->generic
.name
= HeapAlloc(GetProcessHeap(),0,strlen(name
) + 1);
1096 strcpy(newDevice
->generic
.name
, name
);
1098 list_init(&newDevice
->effects
);
1099 if(get_ff(device
, &newDevice
->ff
) == S_OK
){
1100 newDevice
->generic
.devcaps
.dwFlags
|= DIDC_FORCEFEEDBACK
;
1102 hr
= FFDeviceGetForceFeedbackCapabilities(newDevice
->ff
, &ffcaps
);
1104 TRACE("FF Capabilities:\n");
1105 TRACE("\tsupportedEffects: 0x%x\n", (unsigned int)ffcaps
.supportedEffects
);
1106 TRACE("\temulatedEffects: 0x%x\n", (unsigned int)ffcaps
.emulatedEffects
);
1107 TRACE("\tsubType: 0x%x\n", (unsigned int)ffcaps
.subType
);
1108 TRACE("\tnumFfAxes: %u\n", (unsigned int)ffcaps
.numFfAxes
);
1109 TRACE("\tffAxes: [");
1110 for(i
= 0; i
< ffcaps
.numFfAxes
; ++i
){
1111 TRACE("%s", osx_ff_axis_name(ffcaps
.ffAxes
[i
]));
1112 if(i
< ffcaps
.numFfAxes
- 1)
1116 TRACE("\tstorageCapacity: %u\n", (unsigned int)ffcaps
.storageCapacity
);
1117 TRACE("\tplaybackCapacity: %u\n", (unsigned int)ffcaps
.playbackCapacity
);
1120 hr
= FFDeviceSendForceFeedbackCommand(newDevice
->ff
, FFSFFC_RESET
);
1122 WARN("FFDeviceSendForceFeedbackCommand(FFSFFC_RESET) failed: %08x\n", hr
);
1124 hr
= FFDeviceSendForceFeedbackCommand(newDevice
->ff
, FFSFFC_SETACTUATORSON
);
1126 WARN("FFDeviceSendForceFeedbackCommand(FFSFFC_SETACTUATORSON) failed: %08x\n", hr
);
1129 memset(axis_map
, 0, sizeof(axis_map
));
1130 get_osx_device_elements(newDevice
, axis_map
);
1132 TRACE("%i axes %i buttons %i povs\n",newDevice
->generic
.devcaps
.dwAxes
,newDevice
->generic
.devcaps
.dwButtons
,newDevice
->generic
.devcaps
.dwPOVs
);
1134 if (newDevice
->generic
.devcaps
.dwButtons
> 128)
1136 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice
->generic
.devcaps
.dwButtons
);
1137 newDevice
->generic
.devcaps
.dwButtons
= 128;
1140 /* Create copy of default data format */
1141 if (!(df
= HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2
.dwSize
))) goto FAILED
;
1142 memcpy(df
, &c_dfDIJoystick2
, c_dfDIJoystick2
.dwSize
);
1144 df
->dwNumObjs
= newDevice
->generic
.devcaps
.dwAxes
+ newDevice
->generic
.devcaps
.dwPOVs
+ newDevice
->generic
.devcaps
.dwButtons
;
1145 if (!(df
->rgodf
= HeapAlloc(GetProcessHeap(), 0, df
->dwNumObjs
* df
->dwObjSize
))) goto FAILED
;
1147 for (i
= 0; i
< newDevice
->generic
.devcaps
.dwAxes
; i
++)
1150 BOOL has_ff
= FALSE
;
1151 switch (axis_map
[i
])
1153 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_X
):
1155 has_ff
= (newDevice
->ff
!= 0) && osx_axis_has_ff(&ffcaps
, FFJOFS_X
);
1157 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Y
):
1159 has_ff
= (newDevice
->ff
!= 0) && osx_axis_has_ff(&ffcaps
, FFJOFS_Y
);
1161 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Z
):
1163 has_ff
= (newDevice
->ff
!= 0) && osx_axis_has_ff(&ffcaps
, FFJOFS_Z
);
1165 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Rx
):
1166 case MAKEUINT64(kHIDPage_Simulation
, kHIDUsage_Sim_Accelerator
):
1168 has_ff
= (newDevice
->ff
!= 0) && osx_axis_has_ff(&ffcaps
, FFJOFS_RX
);
1170 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Ry
):
1171 case MAKEUINT64(kHIDPage_Simulation
, kHIDUsage_Sim_Brake
):
1173 has_ff
= (newDevice
->ff
!= 0) && osx_axis_has_ff(&ffcaps
, FFJOFS_RY
);
1175 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Rz
):
1177 has_ff
= (newDevice
->ff
!= 0) && osx_axis_has_ff(&ffcaps
, FFJOFS_RZ
);
1179 case MAKEUINT64(kHIDPage_GenericDesktop
, kHIDUsage_GD_Slider
):
1180 wine_obj
= 6 + slider_count
;
1181 has_ff
= (newDevice
->ff
!= 0) && osx_axis_has_ff(&ffcaps
, FFJOFS_SLIDER(slider_count
));
1185 if (wine_obj
< 0 ) continue;
1187 memcpy(&df
->rgodf
[idx
], &c_dfDIJoystick2
.rgodf
[wine_obj
], df
->dwObjSize
);
1188 df
->rgodf
[idx
].dwType
= DIDFT_MAKEINSTANCE(wine_obj
) | DIDFT_ABSAXIS
;
1190 df
->rgodf
[idx
].dwFlags
|= DIDOI_FFACTUATOR
;
1194 for (i
= 0; i
< newDevice
->generic
.devcaps
.dwPOVs
; i
++)
1196 memcpy(&df
->rgodf
[idx
], &c_dfDIJoystick2
.rgodf
[i
+ 8], df
->dwObjSize
);
1197 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(i
) | DIDFT_POV
;
1200 for (i
= 0; i
< newDevice
->generic
.devcaps
.dwButtons
; i
++)
1202 memcpy(&df
->rgodf
[idx
], &c_dfDIJoystick2
.rgodf
[i
+ 12], df
->dwObjSize
);
1203 df
->rgodf
[idx
].pguid
= &GUID_Button
;
1204 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(i
) | DIDFT_PSHBUTTON
;
1206 newDevice
->generic
.base
.data_format
.wine_df
= df
;
1208 /* initialize default properties */
1209 get_osx_device_elements_props(newDevice
);
1211 newDevice
->generic
.devcaps
.dwSize
= sizeof(newDevice
->generic
.devcaps
);
1212 newDevice
->generic
.devcaps
.dwFlags
|= DIDC_ATTACHED
;
1213 if (newDevice
->generic
.base
.dinput
->dwVersion
>= 0x0800)
1214 newDevice
->generic
.devcaps
.dwDevType
= DI8DEVTYPE_JOYSTICK
| (DI8DEVTYPEJOYSTICK_STANDARD
<< 8);
1216 newDevice
->generic
.devcaps
.dwDevType
= DIDEVTYPE_JOYSTICK
| (DIDEVTYPEJOYSTICK_TRADITIONAL
<< 8);
1217 newDevice
->generic
.devcaps
.dwFFSamplePeriod
= 0;
1218 newDevice
->generic
.devcaps
.dwFFMinTimeResolution
= 0;
1219 newDevice
->generic
.devcaps
.dwFirmwareRevision
= 0;
1220 newDevice
->generic
.devcaps
.dwHardwareRevision
= 0;
1221 newDevice
->generic
.devcaps
.dwFFDriverVersion
= 0;
1223 if (TRACE_ON(dinput
)) {
1224 TRACE("allocated device %p\n", newDevice
);
1225 _dump_DIDATAFORMAT(newDevice
->generic
.base
.data_format
.wine_df
);
1226 _dump_DIDEVCAPS(&newDevice
->generic
.devcaps
);
1233 hr
= DIERR_OUTOFMEMORY
;
1234 if (newDevice
->ff
) FFReleaseDevice(newDevice
->ff
);
1235 if (newDevice
->elements
) CFRelease(newDevice
->elements
);
1236 if (df
) HeapFree(GetProcessHeap(), 0, df
->rgodf
);
1237 HeapFree(GetProcessHeap(), 0, df
);
1238 release_DataFormat(&newDevice
->generic
.base
.data_format
);
1239 HeapFree(GetProcessHeap(),0,newDevice
->generic
.name
);
1240 HeapFree(GetProcessHeap(),0,newDevice
);
1244 /******************************************************************************
1245 * get_joystick_index : Get the joystick index from a given GUID
1247 static unsigned short get_joystick_index(REFGUID guid
)
1249 GUID wine_joystick
= DInput_Wine_OsX_Joystick_GUID
;
1250 GUID dev_guid
= *guid
;
1251 GUID prod_guid
= *guid
;
1252 IOHIDDeviceRef device
;
1253 int joystick_devices_count
;
1256 wine_joystick
.Data3
= 0;
1259 /* for the standard joystick GUID use index 0 */
1260 if(IsEqualGUID(&GUID_Joystick
,guid
)) return 0;
1262 /* for the wine joystick GUIDs use the index stored in Data3 */
1263 if(IsEqualGUID(&wine_joystick
, &dev_guid
)) return guid
->Data3
;
1265 prod_guid
.Data1
= 0;
1266 if(IsEqualGUID(&DInput_PIDVID_Product_GUID
, &prod_guid
))
1268 joystick_devices_count
= find_joystick_devices();
1269 for(i
= 0; i
< joystick_devices_count
; i
++)
1271 device
= get_device_ref(i
);
1272 if(guid
->Data1
== make_vid_pid(device
))
1280 static HRESULT
joydev_create_device( IDirectInputImpl
*dinput
, REFGUID rguid
, IDirectInputDevice8W
**out
)
1282 unsigned short index
;
1283 int joystick_devices_count
;
1285 TRACE( "%p %s %p\n", dinput
, debugstr_guid( rguid
), out
);
1288 if ((joystick_devices_count
= find_joystick_devices()) == 0)
1289 return DIERR_DEVICENOTREG
;
1291 if ((index
= get_joystick_index(rguid
)) < 0xffff &&
1292 joystick_devices_count
&& index
< joystick_devices_count
)
1297 if (FAILED(hr
= alloc_device( rguid
, dinput
, &This
, index
))) return hr
;
1299 TRACE( "Created a Joystick device (%p)\n", This
);
1301 *out
= &This
->generic
.base
.IDirectInputDevice8W_iface
;
1305 return DIERR_DEVICENOTREG
;
1308 static HRESULT WINAPI
JoystickWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface
, REFGUID rguid
, LPDIPROPHEADER pdiph
)
1310 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
1312 TRACE("(%p)->(%s,%p)\n", This
, debugstr_guid(rguid
), pdiph
);
1313 _dump_DIPROPHEADER(pdiph
);
1315 if (!IS_DIPROP(rguid
)) return DI_OK
;
1317 switch (LOWORD(rguid
)) {
1318 case (DWORD_PTR
) DIPROP_GUIDANDPATH
:
1320 static const WCHAR formatW
[] = {'\\','\\','?','\\','h','i','d','#','v','i','d','_','%','0','4','x','&',
1321 'p','i','d','_','%','0','4','x','&','%','s','_','%','i',0};
1322 static const WCHAR miW
[] = {'m','i',0};
1323 static const WCHAR igW
[] = {'i','g',0};
1326 IOHIDDeviceRef device
= get_device_ref(This
->id
);
1327 LPDIPROPGUIDANDPATH pd
= (LPDIPROPGUIDANDPATH
)pdiph
;
1328 WORD vid
= get_device_property_long(device
, CFSTR(kIOHIDVendorIDKey
));
1329 WORD pid
= get_device_property_long(device
, CFSTR(kIOHIDProductIDKey
));
1332 return DIERR_UNSUPPORTED
;
1334 is_gamepad
= is_xinput_device(&This
->generic
.devcaps
, vid
, pid
);
1335 pd
->guidClass
= GUID_DEVCLASS_HIDCLASS
;
1336 sprintfW(pd
->wszPath
, formatW
, vid
, pid
, is_gamepad
? igW
: miW
, This
->id
);
1338 TRACE("DIPROP_GUIDANDPATH(%s, %s): returning fake path\n", debugstr_guid(&pd
->guidClass
), debugstr_w(pd
->wszPath
));
1343 return JoystickWGenericImpl_GetProperty(iface
, rguid
, pdiph
);
1349 static HRESULT
osx_set_autocenter(JoystickImpl
*This
,
1350 const DIPROPDWORD
*header
)
1355 return DIERR_UNSUPPORTED
;
1357 hr
= osx_to_win32_hresult(FFDeviceSetForceFeedbackProperty(This
->ff
, FFPROP_AUTOCENTER
, &v
));
1358 TRACE("returning: %08x\n", hr
);
1362 static HRESULT
osx_set_ffgain(JoystickImpl
*This
, const DIPROPDWORD
*header
)
1367 return DIERR_UNSUPPORTED
;
1369 hr
= osx_to_win32_hresult(FFDeviceSetForceFeedbackProperty(This
->ff
, FFPROP_FFGAIN
, &v
));
1370 TRACE("returning: %08x\n", hr
);
1374 static HRESULT WINAPI
JoystickWImpl_SetProperty(IDirectInputDevice8W
*iface
,
1375 const GUID
*prop
, const DIPROPHEADER
*header
)
1377 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
1379 TRACE("%p %s %p\n", This
, debugstr_guid(prop
), header
);
1381 switch(LOWORD(prop
))
1383 case (DWORD_PTR
)DIPROP_AUTOCENTER
:
1384 return osx_set_autocenter(This
, (const DIPROPDWORD
*)header
);
1385 case (DWORD_PTR
)DIPROP_FFGAIN
:
1386 return osx_set_ffgain(This
, (const DIPROPDWORD
*)header
);
1389 return JoystickWGenericImpl_SetProperty(iface
, prop
, header
);
1392 static CFUUIDRef
effect_win_to_mac(const GUID
*effect
)
1395 if(IsEqualGUID(&GUID_##X, effect)) \
1396 return kFFEffectType_##X##_ID;
1397 DO_MAP(ConstantForce
)
1403 DO_MAP(SawtoothDown
)
1410 WARN("Unknown effect GUID! %s\n", debugstr_guid(effect
));
1414 static HRESULT WINAPI
JoystickWImpl_CreateEffect(IDirectInputDevice8W
*iface
,
1415 const GUID
*type
, const DIEFFECT
*params
, IDirectInputEffect
**out
,
1418 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
1422 TRACE("(%p)->(%s %p %p %p)\n", This
, debugstr_guid(type
), params
, out
, outer
);
1423 dump_DIEFFECT(params
, type
, 0);
1426 TRACE("No force feedback support\n");
1428 return DIERR_UNSUPPORTED
;
1432 WARN("aggregation not implemented\n");
1434 effect
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
));
1435 effect
->IDirectInputEffect_iface
.lpVtbl
= &EffectVtbl
;
1437 effect
->guid
= *type
;
1438 effect
->device
= This
;
1440 /* Mac's FFEFFECT and Win's DIEFFECT are binary identical. */
1441 hr
= osx_to_win32_hresult(FFDeviceCreateEffect(This
->ff
,
1442 effect_win_to_mac(type
), (FFEFFECT
*)params
, &effect
->effect
));
1444 WARN("FFDeviceCreateEffect failed: %08x\n", hr
);
1445 HeapFree(GetProcessHeap(), 0, effect
);
1449 list_add_tail(&This
->effects
, &effect
->entry
);
1450 *out
= &effect
->IDirectInputEffect_iface
;
1452 TRACE("allocated effect: %p\n", effect
);
1457 static HRESULT WINAPI
JoystickWImpl_SendForceFeedbackCommand(IDirectInputDevice8W
*iface
,
1460 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
1463 TRACE("%p 0x%x\n", This
, flags
);
1468 hr
= osx_to_win32_hresult(FFDeviceSendForceFeedbackCommand(This
->ff
, flags
));
1470 WARN("FFDeviceSendForceFeedbackCommand failed: %08x\n", hr
);
1477 const struct dinput_device joystick_osx_device
= {
1478 "Wine OS X joystick driver",
1480 joydev_create_device
1483 static const IDirectInputDevice8WVtbl JoystickWvt
=
1485 IDirectInputDevice2WImpl_QueryInterface
,
1486 IDirectInputDevice2WImpl_AddRef
,
1487 JoystickWGenericImpl_Release
,
1488 JoystickWGenericImpl_GetCapabilities
,
1489 IDirectInputDevice2WImpl_EnumObjects
,
1490 JoystickWImpl_GetProperty
,
1491 JoystickWImpl_SetProperty
,
1492 IDirectInputDevice2WImpl_Acquire
,
1493 IDirectInputDevice2WImpl_Unacquire
,
1494 JoystickWGenericImpl_GetDeviceState
,
1495 IDirectInputDevice2WImpl_GetDeviceData
,
1496 IDirectInputDevice2WImpl_SetDataFormat
,
1497 IDirectInputDevice2WImpl_SetEventNotification
,
1498 IDirectInputDevice2WImpl_SetCooperativeLevel
,
1499 JoystickWGenericImpl_GetObjectInfo
,
1500 JoystickWGenericImpl_GetDeviceInfo
,
1501 IDirectInputDevice2WImpl_RunControlPanel
,
1502 IDirectInputDevice2WImpl_Initialize
,
1503 JoystickWImpl_CreateEffect
,
1504 IDirectInputDevice2WImpl_EnumEffects
,
1505 IDirectInputDevice2WImpl_GetEffectInfo
,
1506 IDirectInputDevice2WImpl_GetForceFeedbackState
,
1507 JoystickWImpl_SendForceFeedbackCommand
,
1508 IDirectInputDevice2WImpl_EnumCreatedEffectObjects
,
1509 IDirectInputDevice2WImpl_Escape
,
1510 JoystickWGenericImpl_Poll
,
1511 IDirectInputDevice2WImpl_SendDeviceData
,
1512 IDirectInputDevice7WImpl_EnumEffectsInFile
,
1513 IDirectInputDevice7WImpl_WriteEffectToFile
,
1514 JoystickWGenericImpl_BuildActionMap
,
1515 JoystickWGenericImpl_SetActionMap
,
1516 IDirectInputDevice8WImpl_GetImageInfo
1519 static HRESULT WINAPI
effect_QueryInterface(IDirectInputEffect
*iface
,
1520 const GUID
*guid
, void **out
)
1522 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1524 TRACE("%p %s %p\n", This
, debugstr_guid(guid
), out
);
1526 if(IsEqualIID(guid
, &IID_IUnknown
) || IsEqualIID(guid
, &IID_IDirectInputEffect
)){
1528 IDirectInputEffect_AddRef(iface
);
1532 return E_NOINTERFACE
;
1535 static ULONG WINAPI
effect_AddRef(IDirectInputEffect
*iface
)
1537 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1538 ULONG ref
= InterlockedIncrement(&This
->ref
);
1539 TRACE("%p, ref is now: %u\n", This
, ref
);
1543 static ULONG WINAPI
effect_Release(IDirectInputEffect
*iface
)
1545 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1546 ULONG ref
= InterlockedDecrement(&This
->ref
);
1547 TRACE("%p, ref is now: %u\n", This
, ref
);
1550 list_remove(&This
->entry
);
1551 FFDeviceReleaseEffect(This
->device
->ff
, This
->effect
);
1552 HeapFree(GetProcessHeap(), 0, This
);
1558 static HRESULT WINAPI
effect_Initialize(IDirectInputEffect
*iface
, HINSTANCE hinst
,
1559 DWORD version
, const GUID
*guid
)
1561 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1562 TRACE("%p %p 0x%x, %s\n", This
, hinst
, version
, debugstr_guid(guid
));
1566 static HRESULT WINAPI
effect_GetEffectGuid(IDirectInputEffect
*iface
, GUID
*out
)
1568 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1569 TRACE("%p %p\n", This
, out
);
1574 static HRESULT WINAPI
effect_GetParameters(IDirectInputEffect
*iface
,
1575 DIEFFECT
*effect
, DWORD flags
)
1577 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1578 TRACE("%p %p 0x%x\n", This
, effect
, flags
);
1579 return osx_to_win32_hresult(FFEffectGetParameters(This
->effect
, (FFEFFECT
*)effect
, flags
));
1582 static HRESULT WINAPI
effect_SetParameters(IDirectInputEffect
*iface
,
1583 const DIEFFECT
*effect
, DWORD flags
)
1585 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1586 TRACE("%p %p 0x%x\n", This
, effect
, flags
);
1587 dump_DIEFFECT(effect
, &This
->guid
, flags
);
1588 return osx_to_win32_hresult(FFEffectSetParameters(This
->effect
, (FFEFFECT
*)effect
, flags
));
1591 static HRESULT WINAPI
effect_Start(IDirectInputEffect
*iface
, DWORD iterations
,
1594 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1595 TRACE("%p 0x%x 0x%x\n", This
, iterations
, flags
);
1596 return osx_to_win32_hresult(FFEffectStart(This
->effect
, iterations
, flags
));
1599 static HRESULT WINAPI
effect_Stop(IDirectInputEffect
*iface
)
1601 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1602 TRACE("%p\n", This
);
1603 return osx_to_win32_hresult(FFEffectStop(This
->effect
));
1606 static HRESULT WINAPI
effect_GetEffectStatus(IDirectInputEffect
*iface
, DWORD
*flags
)
1608 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1609 TRACE("%p %p\n", This
, flags
);
1610 return osx_to_win32_hresult(FFEffectGetEffectStatus(This
->effect
, (UInt32
*)flags
));
1613 static HRESULT WINAPI
effect_Download(IDirectInputEffect
*iface
)
1615 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1616 TRACE("%p\n", This
);
1617 return osx_to_win32_hresult(FFEffectDownload(This
->effect
));
1620 static HRESULT WINAPI
effect_Unload(IDirectInputEffect
*iface
)
1622 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1623 TRACE("%p\n", This
);
1624 return osx_to_win32_hresult(FFEffectUnload(This
->effect
));
1627 static HRESULT WINAPI
effect_Escape(IDirectInputEffect
*iface
, DIEFFESCAPE
*escape
)
1629 EffectImpl
*This
= impl_from_IDirectInputEffect(iface
);
1630 TRACE("%p %p\n", This
, escape
);
1631 return osx_to_win32_hresult(FFEffectEscape(This
->effect
, (FFEFFESCAPE
*)escape
));
1634 static const IDirectInputEffectVtbl EffectVtbl
= {
1635 effect_QueryInterface
,
1639 effect_GetEffectGuid
,
1640 effect_GetParameters
,
1641 effect_SetParameters
,
1644 effect_GetEffectStatus
,
1650 #else /* HAVE_IOHIDMANAGERCREATE */
1652 const struct dinput_device joystick_osx_device
= {
1653 "Wine OS X joystick driver",
1658 #endif /* HAVE_IOHIDMANAGERCREATE */