nsiproxy: Implement IPv4 ipstats get_all_parameters.
[wine.git] / dlls / dinput / joystick_osx.c
blob27a47b07639a56ea39cebce1e3835c11be296f45
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 "devguid.h"
89 #include "dinput.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;
106 struct JoystickImpl
108 struct JoystickGenericImpl generic;
110 /* osx private */
111 int id;
112 CFArrayRef elements;
113 ObjProps **propmap;
114 FFDeviceObjectReference ff;
115 struct list effects;
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;
126 LONG ref;
128 JoystickImpl *device;
129 FFEffectObjectReference effect;
130 GUID guid;
132 struct list entry;
133 } EffectImpl;
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 */
150 switch(in){
151 case 0x80000001:
152 return E_NOTIMPL;
153 case 0x80000002:
154 return E_OUTOFMEMORY;
155 case 0x80000003:
156 return E_INVALIDARG;
157 case 0x80000004:
158 return E_NOINTERFACE;
159 case 0x80000005:
160 return E_POINTER;
161 case 0x80000006:
162 return E_HANDLE;
163 case 0x80000007:
164 return E_ABORT;
165 case 0x80000008:
166 return E_FAIL;
167 case 0x80000009:
168 return E_ACCESSDENIED;
169 case 0x8000FFFF:
170 return E_UNEXPECTED;
172 return in;
175 static long get_device_property_long(IOHIDDeviceRef device, CFStringRef key)
177 CFTypeRef ref;
178 long result = 0;
180 if (device)
182 assert(IOHIDDeviceGetTypeID() == CFGetTypeID(device));
184 ref = IOHIDDeviceGetProperty(device, key);
186 if (ref && CFNumberGetTypeID() == CFGetTypeID(ref))
187 CFNumberGetValue((CFNumberRef)ref, kCFNumberLongType, &result);
190 return result;
193 static CFStringRef copy_device_name(IOHIDDeviceRef device)
195 CFStringRef name;
197 if (device)
199 CFTypeRef ref_name;
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);
207 else
209 long vendID, prodID;
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);
216 else
218 ERR("NULL device\n");
219 name = CFStringCreateCopy(kCFAllocatorDefault, CFSTR(""));
222 return name;
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));
239 CFRelease(name1);
240 CFRelease(name2);
241 return result;
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);
249 if (loc1 < loc2)
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)
259 CFStringRef s;
260 const char* ret;
262 if (!t) return "(null)";
264 if (CFGetTypeID(t) == CFStringGetTypeID())
265 s = t;
266 else
267 s = CFCopyDescription(t);
268 ret = CFStringGetCStringPtr(s, kCFStringEncodingUTF8);
269 if (ret) ret = debugstr_a(ret);
270 if (!ret)
272 const UniChar* u = CFStringGetCharactersPtr(s);
273 if (u)
274 ret = debugstr_wn((const WCHAR*)u, CFStringGetLength(s));
276 if (!ret)
278 UniChar buf[200];
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);
284 return ret;
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))
309 return 0;
311 device_main_element = (IOHIDElementRef)CFArrayGetValueAtIndex(device_main_elements, id);
312 if (!device_main_element)
314 ERR("Invalid Element requested %i\n",id);
315 return 0;
318 hid_device = IOHIDElementGetDevice(device_main_element);
319 if (!hid_device)
321 ERR("Invalid Device requested %i\n",id);
322 return 0;
325 TRACE("-> %s\n", debugstr_device(hid_device));
326 return hid_device;
329 static HRESULT get_ff(IOHIDDeviceRef device, FFDeviceObjectReference *ret)
331 io_service_t service;
332 CFMutableDictionaryRef matching;
333 CFTypeRef location_id;
334 HRESULT hr;
336 TRACE("device %s\n", debugstr_device(device));
338 matching = IOServiceMatching(kIOHIDDeviceKey);
339 if(!matching){
340 WARN("IOServiceMatching failed, force feedback disabled\n");
341 return DIERR_DEVICENOTREG;
344 location_id = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDLocationIDKey));
345 if(!location_id){
346 CFRelease(matching);
347 WARN("IOHIDDeviceGetProperty failed, force feedback disabled\n");
348 return DIERR_DEVICENOTREG;
351 CFDictionaryAddValue(matching, CFSTR(kIOHIDLocationIDKey), location_id);
353 service = IOServiceGetMatchingService(kIOMasterPortDefault, matching);
355 if (ret)
356 hr = osx_to_win32_hresult(FFCreateDevice(service, ret));
357 else
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);
362 return hr;
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 );
374 if ( result )
376 int number = kHIDPage_GenericDesktop;
377 CFNumberRef page = CFNumberCreate( kCFAllocatorDefault,
378 kCFNumberIntType, &number);
380 if (page)
382 CFNumberRef cf_usage;
384 CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsagePageKey ), page );
385 CFRelease( page );
387 cf_usage = CFNumberCreate( kCFAllocatorDefault,
388 kCFNumberIntType, &usage);
389 if (cf_usage)
391 CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsageKey ), cf_usage );
392 CFRelease( cf_usage );
394 else
396 ERR("CFNumberCreate() failed.\n");
397 CFRelease(result);
398 return NULL;
401 else
403 ERR("CFNumberCreate failed.\n");
404 CFRelease(result);
405 return NULL;
408 else
410 ERR("CFDictionaryCreateMutable failed.\n");
411 return NULL;
414 return result;
417 static CFIndex find_top_level(IOHIDDeviceRef hid_device, CFMutableArrayRef main_elements)
419 CFArrayRef elements;
420 CFIndex total = 0;
422 TRACE("hid_device %s\n", debugstr_device(hid_device));
424 if (!hid_device)
425 return 0;
427 elements = IOHIDDeviceCopyMatchingElements(hid_device, NULL, 0);
429 if (elements)
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);
449 total++;
453 CFRelease(elements);
456 TRACE("-> total %d\n", (int)total);
457 return total;
460 static void get_element_children(IOHIDElementRef element, CFMutableArrayRef all_children)
462 CFIndex idx, cnt;
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);
478 else
479 CFArrayAppendValue(all_children, child);
483 static int find_osx_devices(void)
485 IOHIDManagerRef hid_manager;
486 CFMutableDictionaryRef result;
487 CFSetRef devset;
488 CFMutableArrayRef matching;
490 TRACE("()\n");
492 hid_manager = IOHIDManagerCreate( kCFAllocatorDefault, 0L );
493 if (IOHIDManagerOpen( hid_manager, 0 ) != kIOReturnSuccess)
495 ERR("Couldn't open IOHIDManager.\n");
496 CFRelease( hid_manager );
497 return 0;
500 matching = CFArrayCreateMutable( kCFAllocatorDefault, 0,
501 &kCFTypeArrayCallBacks );
503 /* build matching dictionary */
504 result = create_osx_device_match(kHIDUsage_GD_Joystick);
505 if (!result)
507 CFRelease(matching);
508 goto fail;
510 CFArrayAppendValue( matching, result );
511 CFRelease( result );
512 result = create_osx_device_match(kHIDUsage_GD_GamePad);
513 if (!result)
515 CFRelease(matching);
516 goto fail;
518 CFArrayAppendValue( matching, result );
519 CFRelease( result );
521 IOHIDManagerSetDeviceMatchingMultiple( hid_manager, matching);
522 CFRelease( matching );
523 devset = IOHIDManagerCopyDevices( hid_manager );
524 if (devset)
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);
532 CFRelease(devset);
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 );
539 goto fail;
542 num_main_elements = 0;
543 for (idx = 0; idx < num_devices; idx++)
545 CFIndex top;
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;
554 CFRelease(devices);
556 TRACE("found %i device(s), %i collection(s)\n",(int)num_devices,(int)num_main_elements);
557 return (int)num_main_elements;
560 fail:
561 IOHIDManagerClose( hid_manager, 0 );
562 CFRelease( hid_manager );
563 return 0;
566 static int get_osx_device_name(int id, char *name, int length)
568 CFStringRef str;
569 IOHIDDeviceRef hid_device;
571 hid_device = get_device_ref(id);
573 TRACE("id %d hid_device %s\n", id, debugstr_device(hid_device));
575 if (name)
576 name[0] = 0;
578 if (!hid_device)
579 return 0;
581 str = IOHIDDeviceGetProperty(hid_device, CFSTR( kIOHIDProductKey ));
582 if (str)
584 CFIndex len = CFStringGetLength(str);
585 if (length >= len)
587 CFStringGetCString(str,name,length,kCFStringEncodingASCII);
588 return len;
590 else
591 return (len+1);
593 return 0;
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);
601 if (usage1 < usage2)
602 return kCFCompareLessThan;
603 if (usage1 > usage2)
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;
612 DWORD sliders = 0;
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))
620 return;
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)
625 return;
627 elements = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
628 get_element_children(device_main_element, elements);
630 if (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) */
665 continue;
668 switch(type)
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 */
676 continue;
679 if (CFArrayGetCount(buttons) < 128)
680 CFArrayAppendValue(buttons, element);
681 break;
683 case kIOHIDElementTypeInput_Axis:
685 TRACE("kIOHIDElementTypeInput_Axis\n");
686 CFArrayAppendValue(axes, element);
687 break;
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);
698 break;
700 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_Slider):
701 sliders ++;
702 if (sliders > 2)
703 break;
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");
718 else
719 break;
722 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_* (%d)\n", usage);
723 axis_map[CFArrayGetCount(axes)]=MAKEUINT64(usage_page, usage);
724 CFArrayAppendValue(axes, element);
725 break;
727 default:
728 FIXME("kIOHIDElementTypeInput_Misc / Unhandled usage %i/%i\n", usage_page, usage);
730 break;
732 default:
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;
751 axes = NULL;
753 CFRelease(povs);
754 CFRelease(buttons);
755 CFRelease(elements);
757 else
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))
798 return;
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));
803 if (!hid_device)
804 return;
806 if (device->elements)
808 int button_idx = 0;
809 int pov_idx = 0;
810 int slider_idx = 0;
811 int inst_id;
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));
823 switch(type)
825 case kIOHIDElementTypeInput_Button:
826 TRACE("kIOHIDElementTypeInput_Button\n");
827 if(button_idx < 128)
829 valueRef = NULL;
830 if (IOHIDDeviceGetValue(hid_device, element, &valueRef) != kIOReturnSuccess)
831 return;
832 if (valueRef == NULL)
833 return;
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++);
844 button_idx ++;
846 break;
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");
856 valueRef = NULL;
857 if (IOHIDDeviceGetValue(hid_device, element, &valueRef) != kIOReturnSuccess)
858 return;
859 if (valueRef == NULL)
860 return;
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))
864 newVal = -1;
865 else
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++);
874 pov_idx ++;
875 break;
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):
887 int wine_obj = -1;
889 valueRef = NULL;
890 if (IOHIDDeviceGetValue(hid_device, element, &valueRef) != kIOReturnSuccess)
891 return;
892 if (valueRef == NULL)
893 return;
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");
900 wine_obj = 0;
901 oldVal = device->generic.js.lX;
902 device->generic.js.lX = newVal;
903 break;
904 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_Y):
905 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Y\n");
906 wine_obj = 1;
907 oldVal = device->generic.js.lY;
908 device->generic.js.lY = newVal;
909 break;
910 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_Z):
911 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Z\n");
912 wine_obj = 2;
913 oldVal = device->generic.js.lZ;
914 device->generic.js.lZ = newVal;
915 break;
916 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_Rx):
917 case MAKEUINT64(kHIDPage_Simulation, kHIDUsage_Sim_Accelerator):
918 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Rx\n");
919 wine_obj = 3;
920 oldVal = device->generic.js.lRx;
921 device->generic.js.lRx = newVal;
922 break;
923 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_Ry):
924 case MAKEUINT64(kHIDPage_Simulation, kHIDUsage_Sim_Brake):
925 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Ry\n");
926 wine_obj = 4;
927 oldVal = device->generic.js.lRy;
928 device->generic.js.lRy = newVal;
929 break;
930 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_Rz):
931 TRACE("kIOHIDElementTypeInput_Misc / kHIDUsage_GD_Rz\n");
932 wine_obj = 5;
933 oldVal = device->generic.js.lRz;
934 device->generic.js.lRz = newVal;
935 break;
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;
941 slider_idx ++;
942 break;
944 TRACE("valueRef %s val %d oldVal %d newVal %d\n", debugstr_cf(valueRef), val, oldVal, newVal);
945 if ((wine_obj != -1) &&
946 (oldVal != newVal))
948 inst_id = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
949 queue_event(iface,inst_id,newVal,GetCurrentTime(),device->generic.base.dinput->evsequence++);
952 break;
954 default:
955 FIXME("kIOHIDElementTypeInput_Misc / unhandled usage %i\n", usage);
957 break;
959 default:
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)
979 long vendID, prodID;
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)
989 char name[MAX_PATH];
990 char friendly[32];
991 IOHIDDeviceRef device;
992 BOOL is_joystick;
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) {
1005 if(!device)
1006 return S_FALSE;
1007 if(get_ff(device, NULL) != S_OK)
1008 return S_FALSE;
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 */
1019 if (is_joystick)
1020 lpddi->wUsage = 0x04; /* Joystick */
1021 else
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;
1031 return S_OK;
1034 return S_FALSE;
1037 static const char *osx_ff_axis_name(UInt8 axis)
1039 static char ret[6];
1040 switch(axis){
1041 case FFJOFS_X:
1042 return "FFJOFS_X";
1043 case FFJOFS_Y:
1044 return "FFJOFS_Y";
1045 case FFJOFS_Z:
1046 return "FFJOFS_Z";
1048 sprintf(ret, "%u", (unsigned int)axis);
1049 return ret;
1052 static BOOL osx_axis_has_ff(FFCAPABILITIES *ffcaps, UInt8 axis)
1054 int i;
1055 for(i = 0; i < ffcaps->numFfAxes; ++i)
1056 if(ffcaps->ffAxes[i] == axis)
1057 return TRUE;
1058 return FALSE;
1061 static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, JoystickImpl **out, unsigned short index )
1063 DWORD i;
1064 IOHIDDeviceRef device;
1065 JoystickImpl* newDevice;
1066 char name[MAX_PATH];
1067 HRESULT hr;
1068 LPDIDATAFORMAT df = NULL;
1069 int idx = 0;
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 )))
1077 return hr;
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);
1103 if(SUCCEEDED(hr)){
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)
1113 TRACE(", ");
1115 TRACE("]\n");
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);
1121 if(FAILED(hr))
1122 WARN("FFDeviceSendForceFeedbackCommand(FFSFFC_RESET) failed: %08x\n", hr);
1124 hr = FFDeviceSendForceFeedbackCommand(newDevice->ff, FFSFFC_SETACTUATORSON);
1125 if(FAILED(hr))
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++)
1149 int wine_obj = -1;
1150 BOOL has_ff = FALSE;
1151 switch (axis_map[i])
1153 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_X):
1154 wine_obj = 0;
1155 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_X);
1156 break;
1157 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_Y):
1158 wine_obj = 1;
1159 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_Y);
1160 break;
1161 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_Z):
1162 wine_obj = 2;
1163 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_Z);
1164 break;
1165 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_Rx):
1166 case MAKEUINT64(kHIDPage_Simulation, kHIDUsage_Sim_Accelerator):
1167 wine_obj = 3;
1168 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_RX);
1169 break;
1170 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_Ry):
1171 case MAKEUINT64(kHIDPage_Simulation, kHIDUsage_Sim_Brake):
1172 wine_obj = 4;
1173 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_RY);
1174 break;
1175 case MAKEUINT64(kHIDPage_GenericDesktop, kHIDUsage_GD_Rz):
1176 wine_obj = 5;
1177 has_ff = (newDevice->ff != 0) && osx_axis_has_ff(&ffcaps, FFJOFS_RZ);
1178 break;
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));
1182 slider_count++;
1183 break;
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;
1189 if(has_ff)
1190 df->rgodf[idx].dwFlags |= DIDOI_FFACTUATOR;
1191 ++idx;
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);
1215 else
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);
1229 *out = newDevice;
1230 return DI_OK;
1232 FAILED:
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);
1241 return hr;
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;
1254 INT i;
1256 wine_joystick.Data3 = 0;
1257 dev_guid.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))
1273 return i;
1277 return 0xffff;
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 );
1286 *out = NULL;
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)
1294 JoystickImpl *This;
1295 HRESULT hr;
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;
1302 return hr;
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};
1325 BOOL is_gamepad;
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));
1331 if (!pid || !vid)
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));
1339 break;
1342 default:
1343 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
1346 return DI_OK;
1349 static HRESULT osx_set_autocenter(JoystickImpl *This,
1350 const DIPROPDWORD *header)
1352 UInt32 v;
1353 HRESULT hr;
1354 if(!This->ff)
1355 return DIERR_UNSUPPORTED;
1356 v = header->dwData;
1357 hr = osx_to_win32_hresult(FFDeviceSetForceFeedbackProperty(This->ff, FFPROP_AUTOCENTER, &v));
1358 TRACE("returning: %08x\n", hr);
1359 return hr;
1362 static HRESULT osx_set_ffgain(JoystickImpl *This, const DIPROPDWORD *header)
1364 UInt32 v;
1365 HRESULT hr;
1366 if(!This->ff)
1367 return DIERR_UNSUPPORTED;
1368 v = header->dwData;
1369 hr = osx_to_win32_hresult(FFDeviceSetForceFeedbackProperty(This->ff, FFPROP_FFGAIN, &v));
1370 TRACE("returning: %08x\n", hr);
1371 return 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)
1394 #define DO_MAP(X) \
1395 if(IsEqualGUID(&GUID_##X, effect)) \
1396 return kFFEffectType_##X##_ID;
1397 DO_MAP(ConstantForce)
1398 DO_MAP(RampForce)
1399 DO_MAP(Square)
1400 DO_MAP(Sine)
1401 DO_MAP(Triangle)
1402 DO_MAP(SawtoothUp)
1403 DO_MAP(SawtoothDown)
1404 DO_MAP(Spring)
1405 DO_MAP(Damper)
1406 DO_MAP(Inertia)
1407 DO_MAP(Friction)
1408 DO_MAP(CustomForce)
1409 #undef DO_MAP
1410 WARN("Unknown effect GUID! %s\n", debugstr_guid(effect));
1411 return 0;
1414 static HRESULT WINAPI JoystickWImpl_CreateEffect(IDirectInputDevice8W *iface,
1415 const GUID *type, const DIEFFECT *params, IDirectInputEffect **out,
1416 IUnknown *outer)
1418 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
1419 EffectImpl *effect;
1420 HRESULT hr;
1422 TRACE("(%p)->(%s %p %p %p)\n", This, debugstr_guid(type), params, out, outer);
1423 dump_DIEFFECT(params, type, 0);
1425 if(!This->ff){
1426 TRACE("No force feedback support\n");
1427 *out = NULL;
1428 return DIERR_UNSUPPORTED;
1431 if(outer)
1432 WARN("aggregation not implemented\n");
1434 effect = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
1435 effect->IDirectInputEffect_iface.lpVtbl = &EffectVtbl;
1436 effect->ref = 1;
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));
1443 if(FAILED(hr)){
1444 WARN("FFDeviceCreateEffect failed: %08x\n", hr);
1445 HeapFree(GetProcessHeap(), 0, effect);
1446 return hr;
1449 list_add_tail(&This->effects, &effect->entry);
1450 *out = &effect->IDirectInputEffect_iface;
1452 TRACE("allocated effect: %p\n", effect);
1454 return S_OK;
1457 static HRESULT WINAPI JoystickWImpl_SendForceFeedbackCommand(IDirectInputDevice8W *iface,
1458 DWORD flags)
1460 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
1461 HRESULT hr;
1463 TRACE("%p 0x%x\n", This, flags);
1465 if(!This->ff)
1466 return DI_NOEFFECT;
1468 hr = osx_to_win32_hresult(FFDeviceSendForceFeedbackCommand(This->ff, flags));
1469 if(FAILED(hr)){
1470 WARN("FFDeviceSendForceFeedbackCommand failed: %08x\n", hr);
1471 return hr;
1474 return S_OK;
1477 const struct dinput_device joystick_osx_device = {
1478 "Wine OS X joystick driver",
1479 joydev_enum_device,
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)){
1527 *out = iface;
1528 IDirectInputEffect_AddRef(iface);
1529 return S_OK;
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);
1540 return 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);
1549 if(!ref){
1550 list_remove(&This->entry);
1551 FFDeviceReleaseEffect(This->device->ff, This->effect);
1552 HeapFree(GetProcessHeap(), 0, This);
1555 return ref;
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));
1563 return S_OK;
1566 static HRESULT WINAPI effect_GetEffectGuid(IDirectInputEffect *iface, GUID *out)
1568 EffectImpl *This = impl_from_IDirectInputEffect(iface);
1569 TRACE("%p %p\n", This, out);
1570 *out = This->guid;
1571 return S_OK;
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,
1592 DWORD flags)
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,
1636 effect_AddRef,
1637 effect_Release,
1638 effect_Initialize,
1639 effect_GetEffectGuid,
1640 effect_GetParameters,
1641 effect_SetParameters,
1642 effect_Start,
1643 effect_Stop,
1644 effect_GetEffectStatus,
1645 effect_Download,
1646 effect_Unload,
1647 effect_Escape
1650 #else /* HAVE_IOHIDMANAGERCREATE */
1652 const struct dinput_device joystick_osx_device = {
1653 "Wine OS X joystick driver",
1654 NULL,
1655 NULL,
1658 #endif /* HAVE_IOHIDMANAGERCREATE */