/************************************************************************* * * HIDAddDeviceToXML( inDevice ) * * Purpose: Adds a devices info to the HID_device_usage_strings.plist( XML ) file * * Inputs: inDevice - the device * Returns: Boolean - if successful */ static Boolean HIDAddDeviceToXML(IOHIDDeviceRef inIOHIDDeviceRef) { Boolean result = FALSE; if ( HIDIsValidDevice(inIOHIDDeviceRef) ) { CFStringRef vendorCFStringRef = IOHIDDevice_GetManufacturer(inIOHIDDeviceRef); CFStringRef productCFStringRef = IOHIDDevice_GetProduct(inIOHIDDeviceRef); if ( vendorCFStringRef && productCFStringRef ) { #if 0 // don't update the cookie xml file gCookieCFPropertyListRef = hu_XMLLoad( CFSTR( "HID_cookie_strings"), CFSTR("plist") ); if ( gCookieCFPropertyListRef ) { CFMutableDictionaryRef tCFMutableDictionaryRef = CFDictionaryCreateMutableCopy( kCFAllocatorDefault, 0, gCookieCFPropertyListRef); if ( tCFMutableDictionaryRef ) { if ( hu_AddVendorProductToCFDict(tCFMutableDictionaryRef, vendorID, vendorCFStringRef, productID, productCFStringRef) ) { hu_XMLSave( tCFMutableDictionaryRef, CFSTR( "HID_cookie_strings"), CFSTR("plist") ); result = TRUE; } CFRelease(tCFMutableDictionaryRef); } } #endif if ( gUsageCFPropertyListRef ) { CFRelease(gUsageCFPropertyListRef); } gUsageCFPropertyListRef = hu_XMLLoad( CFSTR( "HID_device_usage_strings"), CFSTR("plist") ); if ( gUsageCFPropertyListRef ) { CFMutableDictionaryRef tCFMutableDictionaryRef = CFDictionaryCreateMutableCopy( kCFAllocatorDefault, 0, gUsageCFPropertyListRef); if ( tCFMutableDictionaryRef ) { long vendorID = IOHIDDevice_GetVendorID(inIOHIDDeviceRef); long productID = IOHIDDevice_GetProductID(inIOHIDDeviceRef); if ( hu_AddVendorProductToCFDict(tCFMutableDictionaryRef, vendorID, vendorCFStringRef, productID, productCFStringRef) ) { hu_XMLSave( tCFMutableDictionaryRef, CFSTR( "HID_device_usage_strings"), CFSTR("plist") ); result = TRUE; } CFRelease(tCFMutableDictionaryRef); } } } } return (result); } // HIDAddDeviceToXML
// utility routine to dump device info void HIDDumpDeviceInfo(IOHIDDeviceRef inIOHIDDeviceRef) { char cstring[256]; printf("Device: %p = { ", inIOHIDDeviceRef); char manufacturer[256] = ""; // name of manufacturer CFStringRef tCFStringRef = IOHIDDevice_GetManufacturer(inIOHIDDeviceRef); if (tCFStringRef) { (void) CFStringGetCString(tCFStringRef, manufacturer, sizeof(manufacturer), kCFStringEncodingUTF8); } char product[256] = ""; // name of product tCFStringRef = IOHIDDevice_GetProduct(inIOHIDDeviceRef); if (tCFStringRef) { (void) CFStringGetCString(tCFStringRef, product, sizeof(product), kCFStringEncodingUTF8); } printf("%s - %s, ", manufacturer, product); long vendorID = IOHIDDevice_GetVendorID(inIOHIDDeviceRef); if (vendorID) { if ( HIDGetVendorNameFromVendorID(vendorID, cstring) ) { printf(" vendorID: 0x%04lX (\"%s\"), ", vendorID, cstring); } else { printf(" vendorID: 0x%04lX, ", vendorID); } } long productID = IOHIDDevice_GetProductID(inIOHIDDeviceRef); if (productID) { if ( HIDGetProductNameFromVendorProductID(vendorID, productID, cstring) ) { printf(" productID: 0x%04lX (\"%s\"), ", productID, cstring); } else { printf(" productID: 0x%04lX, ", productID); } } uint32_t usagePage = IOHIDDevice_GetUsagePage(inIOHIDDeviceRef); uint32_t usage = IOHIDDevice_GetUsage(inIOHIDDeviceRef); if (!usagePage || !usage) { usagePage = IOHIDDevice_GetPrimaryUsagePage(inIOHIDDeviceRef); usage = IOHIDDevice_GetPrimaryUsage(inIOHIDDeviceRef); } printf("usage: 0x%04lX:0x%04lX, ", (long unsigned int) usagePage, (long unsigned int) usage); tCFStringRef = HIDCopyUsageName(usagePage, usage); if (tCFStringRef) { (void) CFStringGetCString(tCFStringRef, cstring, sizeof(cstring), kCFStringEncodingUTF8); printf("\"%s\", ", cstring); CFRelease(tCFStringRef); } tCFStringRef = IOHIDDevice_GetTransport(inIOHIDDeviceRef); if (tCFStringRef) { (void) CFStringGetCString(tCFStringRef, cstring, sizeof(cstring), kCFStringEncodingUTF8); printf("Transport: \"%s\", ", cstring); } long vendorIDSource = IOHIDDevice_GetVendorIDSource(inIOHIDDeviceRef); if (vendorIDSource) { printf("VendorIDSource: %ld, ", vendorIDSource); } long version = IOHIDDevice_GetVersionNumber(inIOHIDDeviceRef); if (version) { printf("version: %ld, ", version); } tCFStringRef = IOHIDDevice_GetSerialNumber(inIOHIDDeviceRef); if (tCFStringRef) { (void) CFStringGetCString(tCFStringRef, cstring, sizeof(cstring), kCFStringEncodingUTF8); printf("SerialNumber: \"%s\", ", cstring); } long country = IOHIDDevice_GetCountryCode(inIOHIDDeviceRef); if (country) { printf("CountryCode: %ld, ", country); } long locationID = IOHIDDevice_GetLocationID(inIOHIDDeviceRef); if (locationID) { printf("locationID: 0x%08lX, ", locationID); } #if false CFArrayRef pairs = IOHIDDevice_GetUsagePairs(inIOHIDDeviceRef); if (pairs) { CFIndex idx, cnt = CFArrayGetCount(pairs); for (idx = 0; idx < cnt; idx++) { const void * pair = CFArrayGetValueAtIndex(pairs, idx); CFShow(pair); } } #endif // if false long maxInputReportSize = IOHIDDevice_GetMaxInputReportSize(inIOHIDDeviceRef); if (maxInputReportSize) { printf("MaxInputReportSize: %ld, ", maxInputReportSize); } long maxOutputReportSize = IOHIDDevice_GetMaxOutputReportSize(inIOHIDDeviceRef); if (maxOutputReportSize) { printf("MaxOutputReportSize: %ld, ", maxOutputReportSize); } long maxFeatureReportSize = IOHIDDevice_GetMaxFeatureReportSize(inIOHIDDeviceRef); if (maxFeatureReportSize) { printf("MaxFeatureReportSize: %ld, ", maxOutputReportSize); } long reportInterval = IOHIDDevice_GetReportInterval(inIOHIDDeviceRef); if (reportInterval) { printf("ReportInterval: %ld, ", reportInterval); } IOHIDQueueRef queueRef = IOHIDDevice_GetQueue(inIOHIDDeviceRef); if (queueRef) { printf("queue: %p, ", queueRef); } IOHIDTransactionRef transactionRef = IOHIDDevice_GetTransaction(inIOHIDDeviceRef); if (transactionRef) { printf("transaction: %p, ", transactionRef); } printf("}\n"); fflush(stdout); } // HIDDumpDeviceInfo
/************************************************************************* * * hu_AddDeviceElementToUsageXML( inDevice, inElement ) * * Purpose: add a device and it's elements to our usage( XML ) file * * Inputs: inDevice - the device * inElement - the element * * Returns: Boolean - if successful */ static Boolean hu_AddDeviceElementToUsageXML(IOHIDDeviceRef inIOHIDDeviceRef, IOHIDElementRef inIOHIDElementRef) { Boolean results = FALSE; if ( gUsageCFPropertyListRef ) { CFRelease(gUsageCFPropertyListRef); } gUsageCFPropertyListRef = hu_XMLLoad( CFSTR( "HID_device_usage_strings"), CFSTR("plist") ); if ( gUsageCFPropertyListRef ) { CFMutableDictionaryRef tCFMutableDictionaryRef = CFDictionaryCreateMutableCopy( kCFAllocatorDefault, 0, gUsageCFPropertyListRef); if ( tCFMutableDictionaryRef ) { CFMutableDictionaryRef vendorCFMutableDictionaryRef; CFMutableDictionaryRef productCFMutableDictionaryRef; CFStringRef productKeyCFStringRef; CFStringRef usageKeyCFStringRef; // if the vendor dictionary exists... long vendorID = IOHIDDevice_GetVendorID(inIOHIDDeviceRef); CFStringRef vendorKeyCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%ld"), vendorID); if ( vendorKeyCFStringRef ) { if ( CFDictionaryGetValueIfPresent(tCFMutableDictionaryRef, vendorKeyCFStringRef, (const void **) &vendorCFMutableDictionaryRef) ) { // ...copy it... vendorCFMutableDictionaryRef = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, vendorCFMutableDictionaryRef); } else { // ...otherwise... // ...create it. vendorCFMutableDictionaryRef = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); results = TRUE; } // if the vendor name key doesn't exist... if ( !CFDictionaryContainsKey(vendorCFMutableDictionaryRef, kNameKeyCFStringRef) ) { CFStringRef manCFStringRef = IOHIDDevice_GetManufacturer(inIOHIDDeviceRef); // ...create it. CFDictionaryAddValue(vendorCFMutableDictionaryRef, kNameKeyCFStringRef, manCFStringRef); results = TRUE; } // if the product key exists in the vendor dictionary... long productID = IOHIDDevice_GetProductID(inIOHIDDeviceRef); productKeyCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%ld"), productID); if ( CFDictionaryGetValueIfPresent(vendorCFMutableDictionaryRef, productKeyCFStringRef, (const void **) &productCFMutableDictionaryRef) ) { // ...copy it... productCFMutableDictionaryRef = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, productCFMutableDictionaryRef); } else { // ...otherwise... // ...create it. productCFMutableDictionaryRef = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); results = TRUE; } // if the product name key doesn't exist... if ( !CFDictionaryContainsKey(productCFMutableDictionaryRef, kNameKeyCFStringRef) ) { CFStringRef productCFStringRef = IOHIDDevice_GetProduct(inIOHIDDeviceRef); // ...create it. CFDictionaryAddValue(productCFMutableDictionaryRef, kNameKeyCFStringRef, productCFStringRef); results = TRUE; } // if the usage key doesn't exist in the product dictionary... uint32_t usagePage = IOHIDElementGetUsagePage(inIOHIDElementRef); uint32_t usage = IOHIDElementGetUsagePage(inIOHIDElementRef); usageKeyCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%ld:%ld"), usagePage, usage); if ( usageKeyCFStringRef ) { if ( !CFDictionaryContainsKey(productCFMutableDictionaryRef, usageKeyCFStringRef) ) { // find it's generic name CFStringRef usageCFStringRef = HIDCopyUsageName(usagePage, usage); if ( usageCFStringRef ) { // and add that. CFDictionaryAddValue(productCFMutableDictionaryRef, usageKeyCFStringRef, usageCFStringRef); results = TRUE; CFRelease(usageCFStringRef); } } CFRelease(usageKeyCFStringRef); } if ( vendorCFMutableDictionaryRef ) { if ( productCFMutableDictionaryRef ) { if ( results ) { CFDictionarySetValue(vendorCFMutableDictionaryRef, productKeyCFStringRef, productCFMutableDictionaryRef); } CFRelease(productCFMutableDictionaryRef); } if ( results ) { CFDictionarySetValue(tCFMutableDictionaryRef, vendorKeyCFStringRef, vendorCFMutableDictionaryRef); } CFRelease(vendorCFMutableDictionaryRef); } CFRelease(vendorKeyCFStringRef); } if ( productKeyCFStringRef ) { CFRelease(productKeyCFStringRef); } if ( results ) { hu_XMLSave( tCFMutableDictionaryRef, CFSTR( "HID_device_usage_strings"), CFSTR("plist") ); } CFRelease( tCFMutableDictionaryRef); } } return (results); } // hu_AddDeviceElementToUsageXML