common: adapt use of sc_AppendToPath to match new API
[supercollider.git] / lang / LangPrimSource / HID_Utilities_10_4 / HID_Name_Lookup.c
blob710d34ef2db4b3ce57eeb36e162452f3bb2b0e60
1 /*
2 File: HID_Name_Lookup.c
4 Contains: Implementation of the HID device name lookup functions for the HID utilites.
6 DRI: George Warner
8 Copyright: Copyright © 2002 Apple Computer, Inc., All Rights Reserved
10 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
11 ("Apple") in consideration of your agreement to the following terms, and your
12 use, installation, modification or redistribution of this Apple software
13 constitutes acceptance of these terms. If you do not agree with these terms,
14 please do not use, install, modify or redistribute this Apple software.
16 In consideration of your agreement to abide by the following terms, and subject
17 to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
18 copyrights in this original Apple software (the "Apple Software"), to use,
19 reproduce, modify and redistribute the Apple Software, with or without
20 modifications, in source and/or binary forms; provided that if you redistribute
21 the Apple Software in its entirety and without modifications, you must retain
22 this notice and the following text and disclaimers in all such redistributions of
23 the Apple Software. Neither the name, trademarks, service marks or logos of
24 Apple Computer, Inc. may be used to endorse or promote products derived from the
25 Apple Software without specific prior written permission from Apple. Except as
26 expressly stated in this notice, no other rights or licenses, express or implied,
27 are granted by Apple herein, including but not limited to any patent rights that
28 may be infringed by your derivative works or by other works in which the Apple
29 Software may be incorporated.
31 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
32 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
33 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
35 COMBINATION WITH YOUR PRODUCTS.
37 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
38 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
39 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
41 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
42 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
43 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 #ifdef SC_DARWIN
46 #include "HID_Utilities_Internal.h"
47 #include "HID_Name_Lookup.h"
49 #define FAKE_IT 1 // set true for debugging; returns the vendor, product & cookie (or usage info) as numbers.
51 // ---------------------------------
52 // Load the element strings from the given resource (XML) file into a CFPropertyListRef
53 static CFPropertyListRef xml_load(const CFStringRef pResourceName,const CFStringRef pResourceExtension)
55 CFPropertyListRef tCFPropertyListRef = NULL;
56 CFURLRef resFileCFURLRef = CFBundleCopyResourceURL(CFBundleGetMainBundle(), pResourceName, pResourceExtension, NULL);
58 if (NULL != resFileCFURLRef)
60 CFDataRef resCFDataRef;
62 if (CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, resFileCFURLRef, &resCFDataRef, nil, nil, nil))
64 if (NULL != resCFDataRef)
66 CFStringRef errorString;
68 tCFPropertyListRef = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, resCFDataRef, kCFPropertyListImmutable, &errorString);
69 if (NULL == tCFPropertyListRef)
70 CFShow(errorString);
71 CFRelease(resCFDataRef);
74 CFRelease(resFileCFURLRef);
76 return tCFPropertyListRef;
79 // ---------------------------------
80 // Find an element string in the <HID_cookie_strings.plist> resource (XML) file
81 static Boolean xml_search_cookie(const long pVendorID, const long pProductID, const long pCookie, char* pCstr)
83 static CFPropertyListRef tCFPropertyListRef = NULL;
84 Boolean results = false;
86 if (NULL == tCFPropertyListRef)
87 tCFPropertyListRef = xml_load(CFSTR("HID_cookie_strings"), CFSTR("plist"));
88 if (NULL != tCFPropertyListRef)
90 if (CFDictionaryGetTypeID() == CFGetTypeID(tCFPropertyListRef))
92 CFDictionaryRef vendorCFDictionaryRef;
93 CFStringRef vendorKeyCFStringRef;
94 vendorKeyCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%ld"), pVendorID);
96 if (CFDictionaryGetValueIfPresent(tCFPropertyListRef, vendorKeyCFStringRef, (const void**) &vendorCFDictionaryRef))
98 CFDictionaryRef productCFDictionaryRef;
99 CFStringRef productKeyCFStringRef;
100 CFStringRef vendorCFStringRef;
102 if (CFDictionaryGetValueIfPresent(vendorCFDictionaryRef, CFSTR("Name"), (const void**) &vendorCFStringRef))
104 //CFShow(vendorCFStringRef);
106 productKeyCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%ld"), pProductID);
108 if (CFDictionaryGetValueIfPresent(vendorCFDictionaryRef, productKeyCFStringRef, (const void**) &productCFDictionaryRef))
110 CFStringRef fullCFStringRef = NULL;
111 CFStringRef cookieKeyCFStringRef;
112 CFStringRef productCFStringRef;
113 CFStringRef cookieCFStringRef;
115 if (CFDictionaryGetValueIfPresent(productCFDictionaryRef, CFSTR("Name"), (const void**) &productCFStringRef))
117 //CFShow(productCFStringRef);
119 cookieKeyCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%ld"), pCookie);
121 if (CFDictionaryGetValueIfPresent(productCFDictionaryRef, cookieKeyCFStringRef, (const void**) &cookieCFStringRef))
123 fullCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ %@ %@"),
124 vendorCFStringRef, productCFStringRef, cookieCFStringRef);
125 // CFShow(cookieCFStringRef);
127 #if FAKE_IT
128 else
130 fullCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ %@ #%@"),
131 vendorCFStringRef, productCFStringRef, cookieKeyCFStringRef);
133 #endif
134 if (fullCFStringRef)
136 // CFShow(fullCFStringRef);
137 results = CFStringGetCString(
138 fullCFStringRef, pCstr, CFStringGetLength(fullCFStringRef) * sizeof(UniChar) + 1, kCFStringEncodingMacRoman);
139 CFRelease(fullCFStringRef);
141 CFRelease(cookieKeyCFStringRef);
143 CFRelease(productKeyCFStringRef);
145 CFRelease(vendorKeyCFStringRef);
147 //++CFRelease(tCFPropertyListRef); // Leak this!
149 return results;
152 // ---------------------------------
153 // Find an element string in the <HID_device_usage_strings.plist> resource (XML) file
154 static Boolean xml_search_usage(const long pVendorID, const long pProductID, const long pUsagePage, const long pUsage, char* pCstr)
156 static CFPropertyListRef tCFPropertyListRef = NULL;
157 Boolean results = false;
159 if (NULL == tCFPropertyListRef)
160 tCFPropertyListRef = xml_load(CFSTR("HID_device_usage_strings"), CFSTR("plist"));
161 if (NULL != tCFPropertyListRef)
163 if (CFDictionaryGetTypeID() == CFGetTypeID(tCFPropertyListRef))
165 CFDictionaryRef vendorCFDictionaryRef;
166 CFStringRef vendorKeyCFStringRef;
167 vendorKeyCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%ld"), pVendorID);
169 if (CFDictionaryGetValueIfPresent(tCFPropertyListRef, vendorKeyCFStringRef, (const void**) &vendorCFDictionaryRef))
171 CFDictionaryRef productCFDictionaryRef;
172 CFStringRef productKeyCFStringRef;
173 CFStringRef vendorCFStringRef;
175 if (!CFDictionaryGetValueIfPresent(vendorCFDictionaryRef, CFSTR("Name"), (const void**) &vendorCFStringRef))
177 vendorCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("v: %ld"), pVendorID);
178 //CFShow(vendorCFStringRef);
180 productKeyCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%ld"), pProductID);
182 if (CFDictionaryGetValueIfPresent(vendorCFDictionaryRef, productKeyCFStringRef, (const void**) &productCFDictionaryRef))
184 CFStringRef fullCFStringRef = NULL;
185 CFStringRef usageKeyCFStringRef;
186 CFStringRef productCFStringRef;
187 CFStringRef usageCFStringRef;
189 if (CFDictionaryGetValueIfPresent(productCFDictionaryRef, CFSTR("Name"), (const void**) &productCFStringRef))
191 //CFShow(productCFStringRef);
193 usageKeyCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%ld:%ld"), pUsagePage, pUsage);
195 if (CFDictionaryGetValueIfPresent(productCFDictionaryRef, usageKeyCFStringRef, (const void**) &usageCFStringRef))
197 fullCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ %@ %@"),
198 vendorCFStringRef, productCFStringRef, usageCFStringRef);
199 // CFShow(usageCFStringRef);
201 #if FAKE_IT
202 else
204 fullCFStringRef = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ %@ #%@"),
205 vendorCFStringRef, productCFStringRef, usageKeyCFStringRef);
207 #endif
208 if (fullCFStringRef)
210 // CFShow(fullCFStringRef);
211 results = CFStringGetCString(
212 fullCFStringRef, pCstr, CFStringGetLength(fullCFStringRef) * sizeof(UniChar) + 1, kCFStringEncodingMacRoman);
213 CFRelease(fullCFStringRef);
215 CFRelease(usageKeyCFStringRef);
217 CFRelease(productKeyCFStringRef);
219 CFRelease(vendorKeyCFStringRef);
221 //++CFRelease(tCFPropertyListRef); // Leak this!
223 return results;
226 // ---------------------------------
227 // set name from vendor id/product id look up
228 Boolean HIDGetElementNameFromVendorProductCookie (const long pVendorID, const long pProductID, const long pCookie, char * pName)
230 Boolean result = false;
231 *pName = 0; // clear name
233 if (xml_search_cookie(pVendorID, pProductID, pCookie, pName))
234 return true;
236 switch (pVendorID) {
237 case kMacally:
238 switch (pProductID) {
239 case kiShock:
240 result = true;
241 switch (pCookie) {
242 case 3: sprintf(pName, "D-Pad Up"); break;
243 case 4: sprintf(pName, "D-Pad Down"); break;
244 case 5: sprintf(pName, "D-Pad Left"); break;
245 case 6: sprintf(pName, "D-Pad Right"); break;
246 case 7: sprintf(pName, "Up Button"); break;
247 case 8: sprintf(pName, "Right Button"); break;
248 case 9: sprintf(pName, "Down Button"); break;
249 case 10: sprintf(pName, "Left Button"); break;
250 case 11: sprintf(pName, "C Button"); break;
251 case 12: sprintf(pName, "B Button [Select]"); break;
252 case 13: sprintf(pName, "A Button [Start]"); break;
253 case 14: sprintf(pName, "F Button"); break;
254 case 15: sprintf(pName, "R1 Trigger"); break;
255 case 16: sprintf(pName, "R2 Trigger"); break;
256 case 17: sprintf(pName, "L1 Trigger"); break;
257 case 18: sprintf(pName, "L2 Trigger"); break;
258 case 19: sprintf(pName, "Left Stick Button"); break;
259 case 20: sprintf(pName, "Right Stick Button"); break;
260 case 21: sprintf(pName, "D Button"); break;
261 case 22: sprintf(pName, "E Button"); break;
262 case 23: sprintf(pName, "Left Stick X-Axis"); break;
263 case 24: sprintf(pName, "Left Stick Y-Axis"); break;
264 case 25: sprintf(pName, "Right Stick X-Axis"); break;
265 case 26: sprintf(pName, "Right Stick Y-Axis"); break;
266 default:
267 #if FAKE_IT
268 sprintf(pName, "#{V:Macally, P:iShock, C:%ld}#", pCookie);
269 #else
270 result = false;
271 #endif FAKE_IT
272 break;
274 break;
275 default:
276 #if FAKE_IT
277 sprintf(pName, "#{V:Macally, P:%ld, C:%ld}#", pProductID, pCookie); break;
278 #else
279 result = false;
280 #endif FAKE_IT
281 break;
283 break;
284 case kMacsense:
285 switch (pProductID) {
286 case kFunPadF107:
287 result = true;
288 switch (pCookie) {
289 case 3: sprintf(pName, "Button 1"); break;
290 case 4: sprintf(pName, "Button 2"); break;
291 case 5: sprintf(pName, "Button 3"); break;
292 case 6: sprintf(pName, "Button 4"); break;
293 case 7: sprintf(pName, "L1 Trigger"); break;
294 case 8: sprintf(pName, "R1 Trigger"); break;
295 case 9: sprintf(pName, "L2 Trigger"); break;
296 case 10: sprintf(pName, "R2 Trigger"); break;
297 case 11: sprintf(pName, "Right Stick X-Axis"); break;
298 case 12: sprintf(pName, "Right Stick Y-Axis"); break;
299 case 13: sprintf(pName, "Left Stick X-Axis"); break;
300 case 14: sprintf(pName, "Left Stick Y-Axis"); break;
301 case 15: sprintf(pName, "Hat Switch"); break;
302 default:
303 #if FAKE_IT
304 sprintf(pName, "#{V:Macsense, P:FunPad F-107, C:%ld}#", pCookie);
305 #else
306 result = false;
307 #endif FAKE_IT
308 break;
310 default:
311 #if FAKE_IT
312 sprintf(pName, "#{V:Macsense, P:%ld, C:%ld}#", pProductID, pCookie);
313 #else
314 result = false;
315 #endif FAKE_IT
316 break;
318 break;
319 default:
320 #if FAKE_IT
321 sprintf(pName, "#{V:%ld, P:%ld, C:%ld}#", pVendorID, pProductID, pCookie);
322 #else
323 result = false;
324 #endif FAKE_IT
325 break;
327 return result;
331 // ---------------------------------
332 // set name from vendor id/product id & usage look up
333 Boolean HIDGetElementNameFromVendorProductUsage (const long pVendorID, const long pProductID, const long pUsagePage, const long pUsage, char * pName)
335 Boolean result = false;
336 *pName = 0; // clear name
338 if (xml_search_usage(pVendorID, pProductID, pUsagePage, pUsage, pName))
339 return true;
341 #if FAKE_IT
342 sprintf(pName, "#{V:%ld, P:%ld, U:%ld:%ld}#", pVendorID, pProductID, pUsagePage, pUsage);
343 result = true;
344 #endif
346 return result;
348 #endif //SC_DARWIN