mshtml: Rename fire_event_obj and dispatch_event.
[wine.git] / dlls / dinput / joystick_osx.c
blob43fa20ea94c09336fba4b40d2a848006c3bea37e
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
23 #include "config.h"
24 #include "wine/port.h"
26 #if defined(HAVE_IOKIT_HID_IOHIDLIB_H)
27 #define DWORD UInt32
28 #define LPDWORD UInt32*
29 #define LONG SInt32
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>
55 #undef ULONG
56 #undef E_INVALIDARG
57 #undef E_OUTOFMEMORY
58 #undef E_HANDLE
59 #undef E_ACCESSDENIED
60 #undef E_UNEXPECTED
61 #undef E_FAIL
62 #undef E_ABORT
63 #undef E_POINTER
64 #undef E_NOINTERFACE
65 #undef E_NOTIMPL
66 #undef S_FALSE
67 #undef S_OK
68 #undef HRESULT_FACILITY
69 #undef IS_ERROR
70 #undef FAILED
71 #undef SUCCEEDED
72 #undef MAKE_HRESULT
73 #undef HRESULT
74 #undef STDMETHODCALLTYPE
75 #undef DWORD
76 #undef LPDWORD
77 #undef LONG
78 #undef LPLONG
79 #undef E_PENDING
80 #endif /* HAVE_IOKIT_HID_IOHIDLIB_H */
82 #include "wine/debug.h"
83 #include "wine/unicode.h"
84 #include "windef.h"
85 #include "winbase.h"
86 #include "winerror.h"
87 #include "winreg.h"
88 #include "dinput.h"
90 #include "dinput_private.h"
91 #include "device_private.h"
92 #include "joystick_private.h"
94 #ifdef HAVE_IOHIDMANAGERCREATE
96 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
98 static CFMutableArrayRef device_main_elements = NULL;
100 typedef struct JoystickImpl JoystickImpl;
101 static const IDirectInputDevice8AVtbl JoystickAvt;
102 static const IDirectInputDevice8WVtbl JoystickWvt;
104 struct JoystickImpl
106 struct JoystickGenericImpl generic;
108 /* osx private */
109 int id;
110 CFArrayRef elements;
111 ObjProps **propmap;
112 FFDeviceObjectReference ff;
113 struct list effects;
116 static inline JoystickImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
118 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface),
119 JoystickGenericImpl, base), JoystickImpl, generic);
121 static inline JoystickImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
123 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface),
124 JoystickGenericImpl, base), JoystickImpl, generic);
127 typedef struct _EffectImpl {
128 IDirectInputEffect IDirectInputEffect_iface;
129 LONG ref;
131 JoystickImpl *device;
132 FFEffectObjectReference effect;
133 GUID guid;
135 struct list entry;
136 } EffectImpl;
138 static EffectImpl *impl_from_IDirectInputEffect(IDirectInputEffect *iface)
140 return CONTAINING_RECORD(iface, EffectImpl, IDirectInputEffect_iface);
143 static const IDirectInputEffectVtbl EffectVtbl;
145 static const GUID DInput_Wine_OsX_Joystick_GUID = { /* 59CAD8F6-E617-41E2-8EB7-47B23EEEDC5A */
146 0x59CAD8F6, 0xE617, 0x41E2, {0x8E, 0xB7, 0x47, 0xB2, 0x3E, 0xEE, 0xDC, 0x5A}
149 static HRESULT osx_to_win32_hresult(HRESULT in)
151 /* OSX returns 16-bit COM runtime errors, which we should
152 * convert to win32 */
153 switch(in){
154 case 0x80000001:
155 return E_NOTIMPL;
156 case 0x80000002:
157 return E_OUTOFMEMORY;
158 case 0x80000003:
159 return E_INVALIDARG;
160 case 0x80000004:
161 return E_NOINTERFACE;
162 case 0x80000005:
163 return E_POINTER;
164 case 0x80000006:
165 return E_HANDLE;
166 case 0x80000007:
167 return E_ABORT;
168 case 0x80000008:
169 return E_FAIL;
170 case 0x80000009:
171 return E_ACCESSDENIED;
172 case 0x8000FFFF:
173 return E_UNEXPECTED;
175 return in;
178 static long get_device_property_long(IOHIDDeviceRef device, CFStringRef key)
180 CFTypeRef ref;
181 long result = 0;
183 if (device)
185 assert(IOHIDDeviceGetTypeID() == CFGetTypeID(device));
187 ref = IOHIDDeviceGetProperty(device, key);
189 if (ref && CFNumberGetTypeID() == CFGetTypeID(ref))
190 CFNumberGetValue((CFNumberRef)ref, kCFNumberLongType, &result);
193 return result;
196 static CFStringRef copy_device_name(IOHIDDeviceRef device)
198 CFStringRef name;
200 if (device)
202 CFTypeRef ref_name;
204 assert(IOHIDDeviceGetTypeID() == CFGetTypeID(device));
206 ref_name = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));
208 if (ref_name && CFStringGetTypeID() == CFGetTypeID(ref_name))
209 name = CFStringCreateCopy(kCFAllocatorDefault, ref_name);
210 else
212 long vendID, prodID;
214 vendID = get_device_property_long(device, CFSTR(kIOHIDVendorIDKey));
215 prodID = get_device_property_long(device, CFSTR(kIOHIDProductIDKey));
216 name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("0x%04lx 0x%04lx"), vendID, prodID);
219 else
221 ERR("NULL device\n");
222 name = CFStringCreateCopy(kCFAllocatorDefault, CFSTR(""));
225 return name;
228 static long get_device_location_ID(IOHIDDeviceRef device)
230 return get_device_property_long(device, CFSTR(kIOHIDLocationIDKey));
233 static void copy_set_to_array(const void *value, void *context)
235 CFArrayAppendValue(context, value);
238 static CFComparisonResult device_name_comparator(IOHIDDeviceRef device1, IOHIDDeviceRef device2)
240 CFStringRef name1 = copy_device_name(device1), name2 = copy_device_name(device2);
241 CFComparisonResult result = CFStringCompare(name1, name2, (kCFCompareForcedOrdering | kCFCompareNumerically));
242 CFRelease(name1);
243 CFRelease(name2);
244 return result;
247 static CFComparisonResult device_location_name_comparator(const void *val1, const void *val2, void *context)
249 IOHIDDeviceRef device1 = (IOHIDDeviceRef)val1, device2 = (IOHIDDeviceRef)val2;
250 long loc1 = get_device_location_ID(device1), loc2 = get_device_location_ID(device2);
252 if (loc1 < loc2)
253 return kCFCompareLessThan;
254 else if (loc1 > loc2)
255 return kCFCompareGreaterThan;
256 /* virtual joysticks may not have a kIOHIDLocationIDKey and will default to location ID of 0, this orders virtual joysticks by their name */
257 return device_name_comparator(device1, device2);
260 static const char* debugstr_cf(CFTypeRef t)
262 CFStringRef s;
263 const char* ret;
265 if (!t) return "(null)";
267 if (CFGetTypeID(t) == CFStringGetTypeID())
268 s = t;
269 else
270 s = CFCopyDescription(t);
271 ret = CFStringGetCStringPtr(s, kCFStringEncodingUTF8);
272 if (ret) ret = debugstr_a(ret);
273 if (!ret)
275 const UniChar* u = CFStringGetCharactersPtr(s);
276 if (u)
277 ret = debugstr_wn((const WCHAR*)u, CFStringGetLength(s));
279 if (!ret)
281 UniChar buf[200];
282 int len = min(CFStringGetLength(s), sizeof(buf)/sizeof(buf[0]));
283 CFStringGetCharacters(s, CFRangeMake(0, len), buf);
284 ret = debugstr_wn(buf, len);
286 if (s != t) CFRelease(s);
287 return ret;
290 static const char* debugstr_device(IOHIDDeviceRef device)
292 return wine_dbg_sprintf("<IOHIDDevice %p product %s IOHIDLocationID %lu>", device,
293 debugstr_cf(IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey))),
294 get_device_location_ID(device));
297 static const char* debugstr_element(IOHIDElementRef element)
299 return wine_dbg_sprintf("<IOHIDElement %p type %d usage %u/%u device %p>", element,
300 IOHIDElementGetType(element), IOHIDElementGetUsagePage(element),
301 IOHIDElementGetUsage(element), IOHIDElementGetDevice(element));
304 static IOHIDDeviceRef get_device_ref(int id)
306 IOHIDElementRef device_main_element;
307 IOHIDDeviceRef hid_device;
309 TRACE("id %d\n", id);
311 if (!device_main_elements || id >= CFArrayGetCount(device_main_elements))
312 return 0;
314 device_main_element = (IOHIDElementRef)CFArrayGetValueAtIndex(device_main_elements, id);
315 if (!device_main_element)
317 ERR("Invalid Element requested %i\n",id);
318 return 0;
321 hid_device = IOHIDElementGetDevice(device_main_element);
322 if (!hid_device)
324 ERR("Invalid Device requested %i\n",id);
325 return 0;
328 TRACE("-> %s\n", debugstr_device(hid_device));
329 return hid_device;
332 static HRESULT get_ff(IOHIDDeviceRef device, FFDeviceObjectReference *ret)
334 io_service_t service;
335 CFMutableDictionaryRef matching;
336 CFTypeRef location_id;
337 HRESULT hr;
339 TRACE("device %s\n", debugstr_device(device));
341 matching = IOServiceMatching(kIOHIDDeviceKey);
342 if(!matching){
343 WARN("IOServiceMatching failed, force feedback disabled\n");
344 return DIERR_DEVICENOTREG;
347 location_id = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDLocationIDKey));
348 if(!location_id){
349 CFRelease(matching);
350 WARN("IOHIDDeviceGetProperty failed, force feedback disabled\n");
351 return DIERR_DEVICENOTREG;
354 CFDictionaryAddValue(matching, CFSTR(kIOHIDLocationIDKey), location_id);
356 service = IOServiceGetMatchingService(kIOMasterPortDefault, matching);
358 if (ret)
359 hr = osx_to_win32_hresult(FFCreateDevice(service, ret));
360 else
361 hr = FFIsForceFeedback(service) == FF_OK ? S_OK : S_FALSE;
363 IOObjectRelease(service);
364 TRACE("-> hr 0x%08x *ret %p\n", hr, ret ? *ret : NULL);
365 return hr;
368 static CFMutableDictionaryRef create_osx_device_match(int usage)
370 CFMutableDictionaryRef result;
372 TRACE("usage %d\n", usage);
374 result = CFDictionaryCreateMutable( kCFAllocatorDefault, 0,
375 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
377 if ( result )
379 int number = kHIDPage_GenericDesktop;
380 CFNumberRef page = CFNumberCreate( kCFAllocatorDefault,
381 kCFNumberIntType, &number);
383 if (page)
385 CFNumberRef cf_usage;
387 CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsagePageKey ), page );
388 CFRelease( page );
390 cf_usage = CFNumberCreate( kCFAllocatorDefault,
391 kCFNumberIntType, &usage);
392 if (cf_usage)
394 CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsageKey ), cf_usage );
395 CFRelease( cf_usage );
397 else
399 ERR("CFNumberCreate() failed.\n");
400 CFRelease(result);
401 return NULL;
404 else
406 ERR("CFNumberCreate failed.\n");
407 CFRelease(result);
408 return NULL;
411 else
413 ERR("CFDictionaryCreateMutable failed.\n");
414 return NULL;
417 return result;
420 static CFIndex find_top_level(IOHIDDeviceRef hid_device, CFMutableArrayRef main_elements)
422 CFArrayRef elements;
423 CFIndex total = 0;
425 TRACE("hid_device %s\n", debugstr_device(hid_device));
427 if (!hid_device)
428 return 0;
430 elements = IOHIDDeviceCopyMatchingElements(hid_device, NULL, 0);
432 if (elements)
434 CFIndex idx, cnt = CFArrayGetCount(elements);
435 for (idx=0; idx<cnt; idx++)
437 IOHIDElementRef element = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, idx);
438 int type = IOHIDElementGetType(element);
440 TRACE("element %s\n", debugstr_element(element));
442 /* Check for top-level gaming device collections */
443 if (type == kIOHIDElementTypeCollection && IOHIDElementGetParent(element) == 0)
445 int usage_page = IOHIDElementGetUsagePage(element);
446 int usage = IOHIDElementGetUsage(element);
448 if (usage_page == kHIDPage_GenericDesktop &&
449 (usage == kHIDUsage_GD_Joystick || usage == kHIDUsage_GD_GamePad))
451 CFArrayAppendValue(main_elements, element);
452 total++;
456 CFRelease(elements);
459 TRACE("-> total %d\n", (int)total);
460 return total;
463 static void get_element_children(IOHIDElementRef element, CFMutableArrayRef all_children)
465 CFIndex idx, cnt;
466 CFArrayRef element_children = IOHIDElementGetChildren(element);
468 TRACE("element %s\n", debugstr_element(element));
470 cnt = CFArrayGetCount(element_children);
472 /* Either add the element to the array or grab its children */
473 for (idx=0; idx<cnt; idx++)
475 IOHIDElementRef child;
477 child = (IOHIDElementRef)CFArrayGetValueAtIndex(element_children, idx);
478 TRACE("child %s\n", debugstr_element(child));
479 if (IOHIDElementGetType(child) == kIOHIDElementTypeCollection)
480 get_element_children(child, all_children);
481 else
482 CFArrayAppendValue(all_children, child);
486 static int find_osx_devices(void)
488 IOHIDManagerRef hid_manager;
489 CFMutableDictionaryRef result;
490 CFSetRef devset;
491 CFMutableArrayRef matching;
493 TRACE("()\n");
495 hid_manager = IOHIDManagerCreate( kCFAllocatorDefault, 0L );
496 if (IOHIDManagerOpen( hid_manager, 0 ) != kIOReturnSuccess)
498 ERR("Couldn't open IOHIDManager.\n");
499 CFRelease( hid_manager );
500 return 0;
503 matching = CFArrayCreateMutable( kCFAllocatorDefault, 0,
504 &kCFTypeArrayCallBacks );
506 /* build matching dictionary */
507 result = create_osx_device_match(kHIDUsage_GD_Joystick);
508 if (!result)
510 CFRelease(matching);
511 goto fail;
513 CFArrayAppendValue( matching, result );
514 CFRelease( result );
515 result = create_osx_device_match(kHIDUsage_GD_GamePad);
516 if (!result)
518 CFRelease(matching);
519 goto fail;
521 CFArrayAppendValue( matching, result );
522 CFRelease( result );
524 IOHIDManagerSetDeviceMatchingMultiple( hid_manager, matching);
525 CFRelease( matching );
526 devset = IOHIDManagerCopyDevices( hid_manager );
527 if (devset)
529 CFIndex num_devices, num_main_elements, idx;
530 CFMutableArrayRef devices;
532 num_devices = CFSetGetCount(devset);
533 devices = CFArrayCreateMutable(kCFAllocatorDefault, num_devices, &kCFTypeArrayCallBacks);
534 CFSetApplyFunction(devset, copy_set_to_array, devices);
535 CFRelease(devset);
536 CFArraySortValues(devices, CFRangeMake(0, num_devices), device_location_name_comparator, NULL);
538 device_main_elements = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
539 if (!device_main_elements)
541 CFRelease( devices );
542 goto fail;
545 num_main_elements = 0;
546 for (idx = 0; idx < num_devices; idx++)
548 CFIndex top;
549 IOHIDDeviceRef hid_device;
551 hid_device = (IOHIDDeviceRef) CFArrayGetValueAtIndex(devices, idx);
552 TRACE("hid_device %s\n", debugstr_device(hid_device));
553 top = find_top_level(hid_device, device_main_elements);
554 num_main_elements += top;
557 CFRelease(devices);
559 TRACE("found %i device(s), %i collection(s)\n",(int)num_devices,(int)num_main_elements);
560 return (int)num_main_elements;
563 fail:
564 IOHIDManagerClose( hid_manager, 0 );
565 CFRelease( hid_manager );
566 return 0;
569 static int get_osx_device_name(int id, char *name, int length)
571 CFStringRef str;
572 IOHIDDeviceRef hid_device;
574 hid_device = get_device_ref(id);
576 TRACE("id %d hid_device %s\n", id, debugstr_device(hid_device));
578 if (name)
579 name[0] = 0;
581 if (!hid_device)
582 return 0;
584 str = IOHIDDeviceGetProperty(hid_device, CFSTR( kIOHIDProductKey ));
585 if (str)
587 CFIndex len = CFStringGetLength(str);
588 if (length >= len)
590 CFStringGetCString(str,name,length,kCFStringEncodingASCII);
591 return len;
593 else
594 return (len+1);
596 return 0;
599 static CFComparisonResult button_usage_comparator(const void *val1, const void *val2, void *context)
601 IOHIDElementRef element1 = (IOHIDElementRef)val1, element2 = (IOHIDElementRef)val2;
602 int usage1 = IOHIDElementGetUsage(element1), usage2 = IOHIDElementGetUsage(element2);
604 if (usage1 < usage2)
605 return kCFCompareLessThan;
606 if (usage1 > usage2)
607 return kCFCompareGreaterThan;
608 return kCFCompareEqualTo;
611 static void get_osx_device_elements(JoystickImpl *device, int axis_map[8])
613 IOHIDElementRef device_main_element;
614 CFMutableArrayRef elements;
615 DWORD sliders = 0;
617 TRACE("device %p device->id %d\n", device, device->id);
619 device->elements = NULL;
621 if (!device_main_elements || device->id >= CFArrayGetCount(device_main_elements))
622 return;
624 device_main_element = (IOHIDElementRef)CFArrayGetValueAtIndex(device_main_elements, device->id);
625 TRACE("device_main_element %s\n", debugstr_element(device_main_element));
626 if (!device_main_element)
627 return;
629 elements = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
630 get_element_children(device_main_element, elements);
632 if (elements)
634 CFIndex idx, cnt = CFArrayGetCount( elements );
635 CFMutableArrayRef axes = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
636 CFMutableArrayRef buttons = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
637 CFMutableArrayRef povs = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
639 for ( idx = 0; idx < cnt; idx++ )
641 IOHIDElementRef element = ( IOHIDElementRef ) CFArrayGetValueAtIndex( elements, idx );
642 int type = IOHIDElementGetType( element );
644 TRACE("element %s\n", debugstr_element(element));
646 switch(type)
648 case kIOHIDElementTypeInput_Button:
650 int usage_page = IOHIDElementGetUsagePage( element );
651 TRACE("kIOHIDElementTypeInput_Button usage_page %d\n", usage_page);
652 if (usage_page != kHIDPage_Button)
654 /* avoid strange elements found on the 360 controller */
655 continue;
658 if (CFArrayGetCount(buttons) < 128)
659 CFArrayAppendValue(buttons, element);
660 break;
662 case kIOHIDElementTypeInput_Axis:
664 TRACE("kIOHIDElementTypeInput_Axis\n");
665 CFArrayAppendValue(axes, element);
666 break;
668 case kIOHIDElementTypeInput_Misc:
670 uint32_t usage = IOHIDElementGetUsage( element );
671 switch(usage)
673 case kHIDUsage_GD_Hatswitch:
675 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Hatswitch\n");
676 CFArrayAppendValue(povs, element);
677 break;
679 case kHIDUsage_GD_Slider:
680 sliders ++;
681 if (sliders > 2)
682 break;
683 /* fallthrough, sliders are axis */
684 case kHIDUsage_GD_X:
685 case kHIDUsage_GD_Y:
686 case kHIDUsage_GD_Z:
687 case kHIDUsage_GD_Rx:
688 case kHIDUsage_GD_Ry:
689 case kHIDUsage_GD_Rz:
691 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_* (%d)\n", usage);
692 axis_map[CFArrayGetCount(axes)]=usage;
693 CFArrayAppendValue(axes, element);
694 break;
696 default:
697 FIXME("kIOHIDElementTypeInput_Misc / Unhandled usage %i\n", usage);
699 break;
701 default:
702 FIXME("Unhandled type %i\n",type);
706 /* Sort buttons into correct order */
707 CFArraySortValues(buttons, CFRangeMake(0, CFArrayGetCount(buttons)), button_usage_comparator, NULL);
709 device->generic.devcaps.dwAxes = CFArrayGetCount(axes);
710 device->generic.devcaps.dwButtons = CFArrayGetCount(buttons);
711 device->generic.devcaps.dwPOVs = CFArrayGetCount(povs);
713 TRACE("axes %u povs %u buttons %u\n", device->generic.devcaps.dwAxes, device->generic.devcaps.dwPOVs,
714 device->generic.devcaps.dwButtons);
716 /* build our element array in the order that dinput expects */
717 CFArrayAppendArray(axes, povs, CFRangeMake(0, device->generic.devcaps.dwPOVs));
718 CFArrayAppendArray(axes, buttons, CFRangeMake(0, device->generic.devcaps.dwButtons));
719 device->elements = axes;
720 axes = NULL;
722 CFRelease(povs);
723 CFRelease(buttons);
724 CFRelease(elements);
726 else
728 device->generic.devcaps.dwAxes = 0;
729 device->generic.devcaps.dwButtons = 0;
730 device->generic.devcaps.dwPOVs = 0;
734 static void get_osx_device_elements_props(JoystickImpl *device)
736 TRACE("device %p\n", device);
738 if (device->elements)
740 CFIndex idx, cnt = CFArrayGetCount( device->elements );
742 for ( idx = 0; idx < cnt; idx++ )
744 IOHIDElementRef element = ( IOHIDElementRef ) CFArrayGetValueAtIndex( device->elements, idx );
746 TRACE("element %s\n", debugstr_element(element));
748 device->generic.props[idx].lDevMin = IOHIDElementGetLogicalMin(element);
749 device->generic.props[idx].lDevMax = IOHIDElementGetLogicalMax(element);
750 device->generic.props[idx].lMin = 0;
751 device->generic.props[idx].lMax = 0xffff;
752 device->generic.props[idx].lDeadZone = 0;
753 device->generic.props[idx].lSaturation = 0;
758 static void poll_osx_device_state(LPDIRECTINPUTDEVICE8A iface)
760 JoystickImpl *device = impl_from_IDirectInputDevice8A(iface);
761 IOHIDElementRef device_main_element;
762 IOHIDDeviceRef hid_device;
764 TRACE("device %p device->id %i\n", device, device->id);
766 if (!device_main_elements || device->id >= CFArrayGetCount(device_main_elements))
767 return;
769 device_main_element = (IOHIDElementRef) CFArrayGetValueAtIndex(device_main_elements, device->id);
770 hid_device = IOHIDElementGetDevice(device_main_element);
771 TRACE("main element %s hid_device %s\n", debugstr_element(device_main_element), debugstr_device(hid_device));
772 if (!hid_device)
773 return;
775 if (device->elements)
777 int button_idx = 0;
778 int pov_idx = 0;
779 int slider_idx = 0;
780 int inst_id;
781 CFIndex idx, cnt = CFArrayGetCount( device->elements );
783 for ( idx = 0; idx < cnt; idx++ )
785 IOHIDValueRef valueRef;
786 int val, oldVal, newVal;
787 IOHIDElementRef element = ( IOHIDElementRef ) CFArrayGetValueAtIndex( device->elements, idx );
788 int type = IOHIDElementGetType( element );
790 TRACE("element %s\n", debugstr_element(element));
792 switch(type)
794 case kIOHIDElementTypeInput_Button:
795 TRACE("kIOHIDElementTypeInput_Button\n");
796 if(button_idx < 128)
798 IOHIDDeviceGetValue(hid_device, element, &valueRef);
799 val = IOHIDValueGetIntegerValue(valueRef);
800 newVal = val ? 0x80 : 0x0;
801 oldVal = device->generic.js.rgbButtons[button_idx];
802 device->generic.js.rgbButtons[button_idx] = newVal;
803 TRACE("valueRef %s val %d oldVal %d newVal %d\n", debugstr_cf(valueRef), val, oldVal, newVal);
804 if (oldVal != newVal)
806 inst_id = DIDFT_MAKEINSTANCE(button_idx) | DIDFT_PSHBUTTON;
807 queue_event(iface,inst_id,newVal,GetCurrentTime(),device->generic.base.dinput->evsequence++);
809 button_idx ++;
811 break;
812 case kIOHIDElementTypeInput_Misc:
814 uint32_t usage = IOHIDElementGetUsage( element );
815 switch(usage)
817 case kHIDUsage_GD_Hatswitch:
819 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Hatswitch\n");
820 IOHIDDeviceGetValue(hid_device, element, &valueRef);
821 val = IOHIDValueGetIntegerValue(valueRef);
822 oldVal = device->generic.js.rgdwPOV[pov_idx];
823 if (val >= 8)
824 newVal = -1;
825 else
826 newVal = val * 4500;
827 device->generic.js.rgdwPOV[pov_idx] = newVal;
828 TRACE("valueRef %s val %d oldVal %d newVal %d\n", debugstr_cf(valueRef), val, oldVal, newVal);
829 if (oldVal != newVal)
831 inst_id = DIDFT_MAKEINSTANCE(pov_idx) | DIDFT_POV;
832 queue_event(iface,inst_id,newVal,GetCurrentTime(),device->generic.base.dinput->evsequence++);
834 pov_idx ++;
835 break;
837 case kHIDUsage_GD_X:
838 case kHIDUsage_GD_Y:
839 case kHIDUsage_GD_Z:
840 case kHIDUsage_GD_Rx:
841 case kHIDUsage_GD_Ry:
842 case kHIDUsage_GD_Rz:
843 case kHIDUsage_GD_Slider:
845 int wine_obj = -1;
847 IOHIDDeviceGetValue(hid_device, element, &valueRef);
848 val = IOHIDValueGetIntegerValue(valueRef);
849 newVal = joystick_map_axis(&device->generic.props[idx], val);
850 switch (usage)
852 case kHIDUsage_GD_X:
853 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_X\n");
854 wine_obj = 0;
855 oldVal = device->generic.js.lX;
856 device->generic.js.lX = newVal;
857 break;
858 case kHIDUsage_GD_Y:
859 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Y\n");
860 wine_obj = 1;
861 oldVal = device->generic.js.lY;
862 device->generic.js.lY = newVal;
863 break;
864 case kHIDUsage_GD_Z:
865 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Z\n");
866 wine_obj = 2;
867 oldVal = device->generic.js.lZ;
868 device->generic.js.lZ = newVal;
869 break;
870 case kHIDUsage_GD_Rx:
871 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Rx\n");
872 wine_obj = 3;
873 oldVal = device->generic.js.lRx;
874 device->generic.js.lRx = newVal;
875 break;
876 case kHIDUsage_GD_Ry:
877 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Ry\n");
878 wine_obj = 4;
879 oldVal = device->generic.js.lRy;
880 device->generic.js.lRy = newVal;
881 break;
882 case kHIDUsage_GD_Rz:
883 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Rz\n");
884 wine_obj = 5;
885 oldVal = device->generic.js.lRz;
886 device->generic.js.lRz = newVal;
887 break;
888 case kHIDUsage_GD_Slider:
889 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Slider\n");
890 wine_obj = 6 + slider_idx;
891 oldVal = device->generic.js.rglSlider[slider_idx];
892 device->generic.js.rglSlider[slider_idx] = newVal;
893 slider_idx ++;
894 break;
896 TRACE("valueRef %s val %d oldVal %d newVal %d\n", debugstr_cf(valueRef), val, oldVal, newVal);
897 if ((wine_obj != -1) &&
898 (oldVal != newVal))
900 inst_id = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
901 queue_event(iface,inst_id,newVal,GetCurrentTime(),device->generic.base.dinput->evsequence++);
904 break;
906 default:
907 FIXME("kIOHIDElementTypeInput_Misc / unhandled usage %i\n", usage);
909 break;
911 default:
912 FIXME("Unhandled type %i\n",type);
918 static INT find_joystick_devices(void)
920 static INT joystick_devices_count = -1;
922 if (joystick_devices_count != -1) return joystick_devices_count;
924 joystick_devices_count = find_osx_devices();
926 return joystick_devices_count;
929 static HRESULT joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
931 TRACE("dwDevType %u dwFlags 0x%08x version 0x%04x id %d\n", dwDevType, dwFlags, version, id);
933 if (id >= find_joystick_devices()) return E_FAIL;
935 if ((dwDevType == 0) ||
936 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
937 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800)))
939 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
940 IOHIDDeviceRef device = get_device_ref(id);
941 if(!device)
942 return S_FALSE;
943 if(get_ff(device, NULL) != S_OK)
944 return S_FALSE;
946 /* Return joystick */
947 lpddi->guidInstance = DInput_Wine_OsX_Joystick_GUID;
948 lpddi->guidInstance.Data3 = id;
949 lpddi->guidProduct = DInput_Wine_OsX_Joystick_GUID;
950 /* we only support traditional joysticks for now */
951 if (version >= 0x0800)
952 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
953 else
954 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
955 sprintf(lpddi->tszInstanceName, "Joystick %d", id);
957 /* get the device name */
958 get_osx_device_name(id, lpddi->tszProductName, MAX_PATH);
960 lpddi->guidFFDriver = GUID_NULL;
961 return S_OK;
964 return S_FALSE;
967 static HRESULT joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
969 char name[MAX_PATH];
970 char friendly[32];
972 TRACE("dwDevType %u dwFlags 0x%08x version 0x%04x id %d\n", dwDevType, dwFlags, version, id);
974 if (id >= find_joystick_devices()) return E_FAIL;
976 if ((dwDevType == 0) ||
977 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
978 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
979 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
980 IOHIDDeviceRef device = get_device_ref(id);
981 if(!device)
982 return S_FALSE;
983 if(get_ff(device, NULL) != S_OK)
984 return S_FALSE;
986 /* Return joystick */
987 lpddi->guidInstance = DInput_Wine_OsX_Joystick_GUID;
988 lpddi->guidInstance.Data3 = id;
989 lpddi->guidProduct = DInput_Wine_OsX_Joystick_GUID;
990 /* we only support traditional joysticks for now */
991 if (version >= 0x0800)
992 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
993 else
994 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
995 sprintf(friendly, "Joystick %d", id);
996 MultiByteToWideChar(CP_ACP, 0, friendly, -1, lpddi->tszInstanceName, MAX_PATH);
997 /* get the device name */
998 get_osx_device_name(id, name, MAX_PATH);
1000 MultiByteToWideChar(CP_ACP, 0, name, -1, lpddi->tszProductName, MAX_PATH);
1001 lpddi->guidFFDriver = GUID_NULL;
1002 return S_OK;
1005 return S_FALSE;
1008 static const char *osx_ff_axis_name(UInt8 axis)
1010 static char ret[6];
1011 switch(axis){
1012 case FFJOFS_X:
1013 return "FFJOFS_X";
1014 case FFJOFS_Y:
1015 return "FFJOFS_Y";
1016 case FFJOFS_Z:
1017 return "FFJOFS_Z";
1019 sprintf(ret, "%u", (unsigned int)axis);
1020 return ret;
1023 static BOOL osx_axis_has_ff(FFCAPABILITIES *ffcaps, UInt8 axis)
1025 int i;
1026 for(i = 0; i < ffcaps->numFfAxes; ++i)
1027 if(ffcaps->ffAxes[i] == axis)
1028 return TRUE;
1029 return FALSE;
1032 static HRESULT alloc_device(REFGUID rguid, IDirectInputImpl *dinput,
1033 JoystickImpl **pdev, unsigned short index)
1035 DWORD i;
1036 IOHIDDeviceRef device;
1037 JoystickImpl* newDevice;
1038 char name[MAX_PATH];
1039 HRESULT hr;
1040 LPDIDATAFORMAT df = NULL;
1041 int idx = 0;
1042 int axis_map[8]; /* max axes */
1043 int slider_count = 0;
1044 FFCAPABILITIES ffcaps;
1046 TRACE("%s %p %p %hu\n", debugstr_guid(rguid), dinput, pdev, index);
1048 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
1049 if (newDevice == 0) {
1050 WARN("out of memory\n");
1051 *pdev = 0;
1052 return DIERR_OUTOFMEMORY;
1055 newDevice->id = index;
1057 newDevice->generic.guidInstance = DInput_Wine_OsX_Joystick_GUID;
1058 newDevice->generic.guidInstance.Data3 = index;
1059 newDevice->generic.guidProduct = DInput_Wine_OsX_Joystick_GUID;
1060 newDevice->generic.joy_polldev = poll_osx_device_state;
1062 /* get the device name */
1063 get_osx_device_name(index, name, MAX_PATH);
1064 TRACE("Name %s\n",name);
1066 /* copy the device name */
1067 newDevice->generic.name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
1068 strcpy(newDevice->generic.name, name);
1070 list_init(&newDevice->effects);
1071 device = get_device_ref(index);
1072 if(get_ff(device, &newDevice->ff) == S_OK){
1073 newDevice->generic.devcaps.dwFlags |= DIDC_FORCEFEEDBACK;
1075 hr = FFDeviceGetForceFeedbackCapabilities(newDevice->ff, &ffcaps);
1076 if(SUCCEEDED(hr)){
1077 TRACE("FF Capabilities:\n");
1078 TRACE("\tsupportedEffects: 0x%x\n", (unsigned int)ffcaps.supportedEffects);
1079 TRACE("\temulatedEffects: 0x%x\n", (unsigned int)ffcaps.emulatedEffects);
1080 TRACE("\tsubType: 0x%x\n", (unsigned int)ffcaps.subType);
1081 TRACE("\tnumFfAxes: %u\n", (unsigned int)ffcaps.numFfAxes);
1082 TRACE("\tffAxes: [");
1083 for(i = 0; i < ffcaps.numFfAxes; ++i){
1084 TRACE("%s", osx_ff_axis_name(ffcaps.ffAxes[i]));
1085 if(i < ffcaps.numFfAxes - 1)
1086 TRACE(", ");
1088 TRACE("]\n");
1089 TRACE("\tstorageCapacity: %u\n", (unsigned int)ffcaps.storageCapacity);
1090 TRACE("\tplaybackCapacity: %u\n", (unsigned int)ffcaps.playbackCapacity);
1093 hr = FFDeviceSendForceFeedbackCommand(newDevice->ff, FFSFFC_RESET);
1094 if(FAILED(hr))
1095 WARN("FFDeviceSendForceFeedbackCommand(FFSFFC_RESET) failed: %08x\n", hr);
1097 hr = FFDeviceSendForceFeedbackCommand(newDevice->ff, FFSFFC_SETACTUATORSON);
1098 if(FAILED(hr))
1099 WARN("FFDeviceSendForceFeedbackCommand(FFSFFC_SETACTUATORSON) failed: %08x\n", hr);
1102 memset(axis_map, 0, sizeof(axis_map));
1103 get_osx_device_elements(newDevice, axis_map);
1105 TRACE("%i axes %i buttons %i povs\n",newDevice->generic.devcaps.dwAxes,newDevice->generic.devcaps.dwButtons,newDevice->generic.devcaps.dwPOVs);
1107 if (newDevice->generic.devcaps.dwButtons > 128)
1109 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
1110 newDevice->generic.devcaps.dwButtons = 128;
1113 newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
1114 newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
1115 newDevice->generic.base.ref = 1;
1116 newDevice->generic.base.dinput = dinput;
1117 newDevice->generic.base.guid = *rguid;
1118 InitializeCriticalSection(&newDevice->generic.base.crit);
1119 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
1121 /* Create copy of default data format */
1122 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
1123 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
1125 df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
1126 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
1128 for (i = 0; i < newDevice->generic.devcaps.dwAxes; i++)
1130 int wine_obj = -1;
1131 BOOL has_ff = FALSE;
1132 switch (axis_map[i])
1134 case kHIDUsage_GD_X:
1135 wine_obj = 0;
1136 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_X);
1137 break;
1138 case kHIDUsage_GD_Y:
1139 wine_obj = 1;
1140 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_Y);
1141 break;
1142 case kHIDUsage_GD_Z:
1143 wine_obj = 2;
1144 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_Z);
1145 break;
1146 case kHIDUsage_GD_Rx:
1147 wine_obj = 3;
1148 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_RX);
1149 break;
1150 case kHIDUsage_GD_Ry:
1151 wine_obj = 4;
1152 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_RY);
1153 break;
1154 case kHIDUsage_GD_Rz:
1155 wine_obj = 5;
1156 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_RZ);
1157 break;
1158 case kHIDUsage_GD_Slider:
1159 wine_obj = 6 + slider_count;
1160 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_SLIDER(slider_count));
1161 slider_count++;
1162 break;
1164 if (wine_obj < 0 ) continue;
1166 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
1167 df->rgodf[idx].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
1168 if(has_ff)
1169 df->rgodf[idx].dwFlags |= DIDOI_FFACTUATOR;
1170 ++idx;
1173 for (i = 0; i < newDevice->generic.devcaps.dwPOVs; i++)
1175 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 8], df->dwObjSize);
1176 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_POV;
1179 for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
1181 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
1182 df->rgodf[idx ].pguid = &GUID_Button;
1183 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
1185 newDevice->generic.base.data_format.wine_df = df;
1187 /* initialize default properties */
1188 get_osx_device_elements_props(newDevice);
1190 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
1192 EnterCriticalSection(&dinput->crit);
1193 list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
1194 LeaveCriticalSection(&dinput->crit);
1196 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
1197 newDevice->generic.devcaps.dwFlags |= DIDC_ATTACHED;
1198 if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
1199 newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
1200 else
1201 newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
1202 newDevice->generic.devcaps.dwFFSamplePeriod = 0;
1203 newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
1204 newDevice->generic.devcaps.dwFirmwareRevision = 0;
1205 newDevice->generic.devcaps.dwHardwareRevision = 0;
1206 newDevice->generic.devcaps.dwFFDriverVersion = 0;
1208 if (TRACE_ON(dinput)) {
1209 TRACE("allocated device %p\n", newDevice);
1210 _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
1211 _dump_DIDEVCAPS(&newDevice->generic.devcaps);
1214 *pdev = newDevice;
1216 return DI_OK;
1218 FAILED:
1219 hr = DIERR_OUTOFMEMORY;
1220 if (newDevice->ff) FFReleaseDevice(newDevice->ff);
1221 if (newDevice->elements) CFRelease(newDevice->elements);
1222 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
1223 HeapFree(GetProcessHeap(), 0, df);
1224 release_DataFormat(&newDevice->generic.base.data_format);
1225 HeapFree(GetProcessHeap(),0,newDevice->generic.name);
1226 HeapFree(GetProcessHeap(),0,newDevice);
1227 *pdev = 0;
1229 return hr;
1232 /******************************************************************************
1233 * get_joystick_index : Get the joystick index from a given GUID
1235 static unsigned short get_joystick_index(REFGUID guid)
1237 GUID wine_joystick = DInput_Wine_OsX_Joystick_GUID;
1238 GUID dev_guid = *guid;
1240 wine_joystick.Data3 = 0;
1241 dev_guid.Data3 = 0;
1243 /* for the standard joystick GUID use index 0 */
1244 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
1246 /* for the wine joystick GUIDs use the index stored in Data3 */
1247 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
1249 return 0xffff;
1252 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
1254 unsigned short index;
1255 int joystick_devices_count;
1257 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
1258 *pdev = NULL;
1260 if ((joystick_devices_count = find_joystick_devices()) == 0)
1261 return DIERR_DEVICENOTREG;
1263 if ((index = get_joystick_index(rguid)) < 0xffff &&
1264 joystick_devices_count && index < joystick_devices_count)
1266 JoystickImpl *This;
1267 HRESULT hr;
1269 if (riid == NULL)
1270 ;/* nothing */
1271 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
1272 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
1273 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
1274 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
1276 unicode = 0;
1278 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
1279 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
1280 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
1281 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
1283 unicode = 1;
1285 else
1287 WARN("no interface\n");
1288 return DIERR_NOINTERFACE;
1291 hr = alloc_device(rguid, dinput, &This, index);
1292 if (!This) return hr;
1294 if (unicode)
1295 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
1296 else
1297 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
1298 return hr;
1301 return DIERR_DEVICENOTREG;
1304 static HRESULT osx_set_autocenter(JoystickImpl *This,
1305 const DIPROPDWORD *header)
1307 UInt32 v;
1308 HRESULT hr;
1309 if(!This->ff)
1310 return DIERR_UNSUPPORTED;
1311 v = header->dwData;
1312 hr = osx_to_win32_hresult(FFDeviceSetForceFeedbackProperty(This->ff, FFPROP_AUTOCENTER, &v));
1313 TRACE("returning: %08x\n", hr);
1314 return hr;
1317 static HRESULT osx_set_ffgain(JoystickImpl *This, const DIPROPDWORD *header)
1319 UInt32 v;
1320 HRESULT hr;
1321 if(!This->ff)
1322 return DIERR_UNSUPPORTED;
1323 v = header->dwData;
1324 hr = osx_to_win32_hresult(FFDeviceSetForceFeedbackProperty(This->ff, FFPROP_FFGAIN, &v));
1325 TRACE("returning: %08x\n", hr);
1326 return hr;
1329 static HRESULT WINAPI JoystickWImpl_SetProperty(IDirectInputDevice8W *iface,
1330 const GUID *prop, const DIPROPHEADER *header)
1332 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
1334 TRACE("%p %s %p\n", This, debugstr_guid(prop), header);
1336 switch(LOWORD(prop))
1338 case (DWORD_PTR)DIPROP_AUTOCENTER:
1339 return osx_set_autocenter(This, (const DIPROPDWORD *)header);
1340 case (DWORD_PTR)DIPROP_FFGAIN:
1341 return osx_set_ffgain(This, (const DIPROPDWORD *)header);
1344 return JoystickWGenericImpl_SetProperty(iface, prop, header);
1347 static HRESULT WINAPI JoystickAImpl_SetProperty(IDirectInputDevice8A *iface,
1348 const GUID *prop, const DIPROPHEADER *header)
1350 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1352 TRACE("%p %s %p\n", This, debugstr_guid(prop), header);
1354 switch(LOWORD(prop))
1356 case (DWORD_PTR)DIPROP_AUTOCENTER:
1357 return osx_set_autocenter(This, (const DIPROPDWORD *)header);
1358 case (DWORD_PTR)DIPROP_FFGAIN:
1359 return osx_set_ffgain(This, (const DIPROPDWORD *)header);
1362 return JoystickAGenericImpl_SetProperty(iface, prop, header);
1365 static CFUUIDRef effect_win_to_mac(const GUID *effect)
1367 #define DO_MAP(X) \
1368 if(IsEqualGUID(&GUID_##X, effect)) \
1369 return kFFEffectType_##X##_ID;
1370 DO_MAP(ConstantForce)
1371 DO_MAP(RampForce)
1372 DO_MAP(Square)
1373 DO_MAP(Sine)
1374 DO_MAP(Triangle)
1375 DO_MAP(SawtoothUp)
1376 DO_MAP(SawtoothDown)
1377 DO_MAP(Spring)
1378 DO_MAP(Damper)
1379 DO_MAP(Inertia)
1380 DO_MAP(Friction)
1381 DO_MAP(CustomForce)
1382 #undef DO_MAP
1383 WARN("Unknown effect GUID! %s\n", debugstr_guid(effect));
1384 return 0;
1387 static HRESULT WINAPI JoystickWImpl_CreateEffect(IDirectInputDevice8W *iface,
1388 const GUID *type, const DIEFFECT *params, IDirectInputEffect **out,
1389 IUnknown *outer)
1391 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
1392 EffectImpl *effect;
1393 HRESULT hr;
1395 TRACE("%p %s %p %p %p\n", iface, debugstr_guid(type), params, out, outer);
1396 dump_DIEFFECT(params, type, 0);
1398 if(!This->ff){
1399 TRACE("No force feedback support\n");
1400 *out = NULL;
1401 return DIERR_UNSUPPORTED;
1404 if(outer)
1405 WARN("aggregation not implemented\n");
1407 effect = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
1408 effect->IDirectInputEffect_iface.lpVtbl = &EffectVtbl;
1409 effect->ref = 1;
1410 effect->guid = *type;
1411 effect->device = This;
1413 /* Mac's FFEFFECT and Win's DIEFFECT are binary identical. */
1414 hr = osx_to_win32_hresult(FFDeviceCreateEffect(This->ff,
1415 effect_win_to_mac(type), (FFEFFECT*)params, &effect->effect));
1416 if(FAILED(hr)){
1417 WARN("FFDeviceCreateEffect failed: %08x\n", hr);
1418 HeapFree(GetProcessHeap(), 0, effect);
1419 return hr;
1422 list_add_tail(&This->effects, &effect->entry);
1423 *out = &effect->IDirectInputEffect_iface;
1425 TRACE("allocated effect: %p\n", effect);
1427 return S_OK;
1430 static HRESULT WINAPI JoystickAImpl_CreateEffect(IDirectInputDevice8A *iface,
1431 const GUID *type, const DIEFFECT *params, IDirectInputEffect **out,
1432 IUnknown *outer)
1434 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1436 TRACE("%p %s %p %p %p\n", iface, debugstr_guid(type), params, out, outer);
1438 return JoystickWImpl_CreateEffect(&This->generic.base.IDirectInputDevice8W_iface,
1439 type, params, out, outer);
1442 static HRESULT WINAPI JoystickWImpl_SendForceFeedbackCommand(IDirectInputDevice8W *iface,
1443 DWORD flags)
1445 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
1446 HRESULT hr;
1448 TRACE("%p 0x%x\n", This, flags);
1450 if(!This->ff)
1451 return DI_NOEFFECT;
1453 hr = osx_to_win32_hresult(FFDeviceSendForceFeedbackCommand(This->ff, flags));
1454 if(FAILED(hr)){
1455 WARN("FFDeviceSendForceFeedbackCommand failed: %08x\n", hr);
1456 return hr;
1459 return S_OK;
1462 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(IDirectInputDevice8A *iface,
1463 DWORD flags)
1465 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
1467 TRACE("%p 0x%x\n", This, flags);
1469 return JoystickWImpl_SendForceFeedbackCommand(&This->generic.base.IDirectInputDevice8W_iface, flags);
1472 const struct dinput_device joystick_osx_device = {
1473 "Wine OS X joystick driver",
1474 joydev_enum_deviceA,
1475 joydev_enum_deviceW,
1476 joydev_create_device
1479 static const IDirectInputDevice8AVtbl JoystickAvt =
1481 IDirectInputDevice2AImpl_QueryInterface,
1482 IDirectInputDevice2AImpl_AddRef,
1483 IDirectInputDevice2AImpl_Release,
1484 JoystickAGenericImpl_GetCapabilities,
1485 IDirectInputDevice2AImpl_EnumObjects,
1486 JoystickAGenericImpl_GetProperty,
1487 JoystickAImpl_SetProperty,
1488 IDirectInputDevice2AImpl_Acquire,
1489 IDirectInputDevice2AImpl_Unacquire,
1490 JoystickAGenericImpl_GetDeviceState,
1491 IDirectInputDevice2AImpl_GetDeviceData,
1492 IDirectInputDevice2AImpl_SetDataFormat,
1493 IDirectInputDevice2AImpl_SetEventNotification,
1494 IDirectInputDevice2AImpl_SetCooperativeLevel,
1495 JoystickAGenericImpl_GetObjectInfo,
1496 JoystickAGenericImpl_GetDeviceInfo,
1497 IDirectInputDevice2AImpl_RunControlPanel,
1498 IDirectInputDevice2AImpl_Initialize,
1499 JoystickAImpl_CreateEffect,
1500 IDirectInputDevice2AImpl_EnumEffects,
1501 IDirectInputDevice2AImpl_GetEffectInfo,
1502 IDirectInputDevice2AImpl_GetForceFeedbackState,
1503 JoystickAImpl_SendForceFeedbackCommand,
1504 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1505 IDirectInputDevice2AImpl_Escape,
1506 JoystickAGenericImpl_Poll,
1507 IDirectInputDevice2AImpl_SendDeviceData,
1508 IDirectInputDevice7AImpl_EnumEffectsInFile,
1509 IDirectInputDevice7AImpl_WriteEffectToFile,
1510 JoystickAGenericImpl_BuildActionMap,
1511 JoystickAGenericImpl_SetActionMap,
1512 IDirectInputDevice8AImpl_GetImageInfo
1515 static const IDirectInputDevice8WVtbl JoystickWvt =
1517 IDirectInputDevice2WImpl_QueryInterface,
1518 IDirectInputDevice2WImpl_AddRef,
1519 IDirectInputDevice2WImpl_Release,
1520 JoystickWGenericImpl_GetCapabilities,
1521 IDirectInputDevice2WImpl_EnumObjects,
1522 JoystickWGenericImpl_GetProperty,
1523 JoystickWImpl_SetProperty,
1524 IDirectInputDevice2WImpl_Acquire,
1525 IDirectInputDevice2WImpl_Unacquire,
1526 JoystickWGenericImpl_GetDeviceState,
1527 IDirectInputDevice2WImpl_GetDeviceData,
1528 IDirectInputDevice2WImpl_SetDataFormat,
1529 IDirectInputDevice2WImpl_SetEventNotification,
1530 IDirectInputDevice2WImpl_SetCooperativeLevel,
1531 JoystickWGenericImpl_GetObjectInfo,
1532 JoystickWGenericImpl_GetDeviceInfo,
1533 IDirectInputDevice2WImpl_RunControlPanel,
1534 IDirectInputDevice2WImpl_Initialize,
1535 JoystickWImpl_CreateEffect,
1536 IDirectInputDevice2WImpl_EnumEffects,
1537 IDirectInputDevice2WImpl_GetEffectInfo,
1538 IDirectInputDevice2WImpl_GetForceFeedbackState,
1539 JoystickWImpl_SendForceFeedbackCommand,
1540 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
1541 IDirectInputDevice2WImpl_Escape,
1542 JoystickWGenericImpl_Poll,
1543 IDirectInputDevice2WImpl_SendDeviceData,
1544 IDirectInputDevice7WImpl_EnumEffectsInFile,
1545 IDirectInputDevice7WImpl_WriteEffectToFile,
1546 JoystickWGenericImpl_BuildActionMap,
1547 JoystickWGenericImpl_SetActionMap,
1548 IDirectInputDevice8WImpl_GetImageInfo
1551 static HRESULT WINAPI effect_QueryInterface(IDirectInputEffect *iface,
1552 const GUID *guid, void **out)
1554 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1556 TRACE("%p %s %p\n", This, debugstr_guid(guid), out);
1558 if(IsEqualIID(guid, &IID_IUnknown) || IsEqualIID(guid, &IID_IDirectInputEffect)){
1559 *out = iface;
1560 IDirectInputEffect_AddRef(iface);
1561 return S_OK;
1564 return E_NOINTERFACE;
1567 static ULONG WINAPI effect_AddRef(IDirectInputEffect *iface)
1569 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1570 ULONG ref = InterlockedIncrement(&This->ref);
1571 TRACE("%p, ref is now: %u\n", This, ref);
1572 return ref;
1575 static ULONG WINAPI effect_Release(IDirectInputEffect *iface)
1577 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1578 ULONG ref = InterlockedDecrement(&This->ref);
1579 TRACE("%p, ref is now: %u\n", This, ref);
1581 if(!ref){
1582 list_remove(&This->entry);
1583 FFDeviceReleaseEffect(This->device->ff, This->effect);
1584 HeapFree(GetProcessHeap(), 0, This);
1587 return ref;
1590 static HRESULT WINAPI effect_Initialize(IDirectInputEffect *iface, HINSTANCE hinst,
1591 DWORD version, const GUID *guid)
1593 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1594 TRACE("%p %p 0x%x, %s\n", This, hinst, version, debugstr_guid(guid));
1595 return S_OK;
1598 static HRESULT WINAPI effect_GetEffectGuid(IDirectInputEffect *iface, GUID *out)
1600 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1601 TRACE("%p %p\n", This, out);
1602 *out = This->guid;
1603 return S_OK;
1606 static HRESULT WINAPI effect_GetParameters(IDirectInputEffect *iface,
1607 DIEFFECT *effect, DWORD flags)
1609 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1610 TRACE("%p %p 0x%x\n", This, effect, flags);
1611 return osx_to_win32_hresult(FFEffectGetParameters(This->effect, (FFEFFECT*)effect, flags));
1614 static HRESULT WINAPI effect_SetParameters(IDirectInputEffect *iface,
1615 const DIEFFECT *effect, DWORD flags)
1617 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1618 TRACE("%p %p 0x%x\n", This, effect, flags);
1619 dump_DIEFFECT(effect, &This->guid, flags);
1620 return osx_to_win32_hresult(FFEffectSetParameters(This->effect, (FFEFFECT*)effect, flags));
1623 static HRESULT WINAPI effect_Start(IDirectInputEffect *iface, DWORD iterations,
1624 DWORD flags)
1626 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1627 TRACE("%p 0x%x 0x%x\n", This, iterations, flags);
1628 return osx_to_win32_hresult(FFEffectStart(This->effect, iterations, flags));
1631 static HRESULT WINAPI effect_Stop(IDirectInputEffect *iface)
1633 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1634 TRACE("%p\n", This);
1635 return osx_to_win32_hresult(FFEffectStop(This->effect));
1638 static HRESULT WINAPI effect_GetEffectStatus(IDirectInputEffect *iface, DWORD *flags)
1640 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1641 TRACE("%p %p\n", This, flags);
1642 return osx_to_win32_hresult(FFEffectGetEffectStatus(This->effect, (UInt32*)flags));
1645 static HRESULT WINAPI effect_Download(IDirectInputEffect *iface)
1647 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1648 TRACE("%p\n", This);
1649 return osx_to_win32_hresult(FFEffectDownload(This->effect));
1652 static HRESULT WINAPI effect_Unload(IDirectInputEffect *iface)
1654 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1655 TRACE("%p\n", This);
1656 return osx_to_win32_hresult(FFEffectUnload(This->effect));
1659 static HRESULT WINAPI effect_Escape(IDirectInputEffect *iface, DIEFFESCAPE *escape)
1661 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1662 TRACE("%p %p\n", This, escape);
1663 return osx_to_win32_hresult(FFEffectEscape(This->effect, (FFEFFESCAPE*)escape));
1666 static const IDirectInputEffectVtbl EffectVtbl = {
1667 effect_QueryInterface,
1668 effect_AddRef,
1669 effect_Release,
1670 effect_Initialize,
1671 effect_GetEffectGuid,
1672 effect_GetParameters,
1673 effect_SetParameters,
1674 effect_Start,
1675 effect_Stop,
1676 effect_GetEffectStatus,
1677 effect_Download,
1678 effect_Unload,
1679 effect_Escape
1682 #else /* HAVE_IOHIDMANAGERCREATE */
1684 const struct dinput_device joystick_osx_device = {
1685 "Wine OS X joystick driver",
1686 NULL,
1687 NULL,
1688 NULL
1691 #endif /* HAVE_IOHIDMANAGERCREATE */