/src/ois/src/mac/MacHIDManager.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 435 lines · 320 code · 62 blank · 53 comment · 80 complexity · 7f43ea4401f6c681f03fec82d59515d6 MD5 · raw file

  1. /*
  2. The zlib/libpng License
  3. Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
  4. This software is provided 'as-is', without any express or implied warranty. In no event will
  5. the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose, including commercial
  7. applications, and to alter it and redistribute it freely, subject to the following
  8. restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that
  10. you wrote the original software. If you use this software in a product,
  11. an acknowledgment in the product documentation would be appreciated but is
  12. not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. #include "mac/MacHIDManager.h"
  18. #include "mac/MacJoyStick.h"
  19. #include "OISException.h"
  20. #include "OISObject.h"
  21. #include <iostream>
  22. using namespace std;
  23. using namespace OIS;
  24. //------------------------------------------------------------------------------------------------------//
  25. //------------------------------------------------------------------------------------------------------//
  26. template<typename T>
  27. T getDictionaryItemAsRef(CFDictionaryRef dict, const char* keyName)
  28. {
  29. return CFDictionaryGetValue(dict, OIS_CFString(keyName));
  30. }
  31. template<>
  32. CFArrayRef getDictionaryItemAsRef(CFDictionaryRef dict, const char* keyName)
  33. {
  34. CFTypeRef temp = CFDictionaryGetValue(dict, OIS_CFString(keyName));
  35. if(temp && CFGetTypeID(temp) == CFArrayGetTypeID())
  36. return (CFArrayRef)temp;
  37. else
  38. return 0;
  39. }
  40. template<>
  41. CFStringRef getDictionaryItemAsRef(CFDictionaryRef dict, const char* keyName)
  42. {
  43. CFTypeRef temp = CFDictionaryGetValue(dict, OIS_CFString(keyName));
  44. if(temp && CFGetTypeID(temp) == CFStringGetTypeID())
  45. return (CFStringRef)temp;
  46. else
  47. return 0;
  48. }
  49. template<>
  50. CFNumberRef getDictionaryItemAsRef(CFDictionaryRef dict, const char* keyName)
  51. {
  52. CFTypeRef temp = CFDictionaryGetValue(dict, OIS_CFString(keyName));
  53. if(temp && CFGetTypeID(temp) == CFNumberGetTypeID())
  54. return (CFNumberRef)temp;
  55. else
  56. return 0;
  57. }
  58. //------------------------------------------------------------------------------------------------------//
  59. //------------------------------------------------------------------------------------------------------//
  60. template<typename T>
  61. T getArrayItemAsRef(CFArrayRef array, CFIndex idx)
  62. {
  63. return CFArrayGetValueAtIndex(array, idx);
  64. }
  65. template<>
  66. CFDictionaryRef getArrayItemAsRef(CFArrayRef array, CFIndex idx)
  67. {
  68. CFTypeRef temp = CFArrayGetValueAtIndex(array, idx);
  69. if(temp && CFGetTypeID(temp) == CFDictionaryGetTypeID())
  70. return (CFDictionaryRef)temp;
  71. else
  72. return 0;
  73. }
  74. //------------------------------------------------------------------------------------------------------//
  75. int getInt32(CFNumberRef ref)
  76. {
  77. int r = 0;
  78. if (r)
  79. CFNumberGetValue(ref, kCFNumberIntType, &r);
  80. return r;
  81. }
  82. //--------------------------------------------------------------------------------//
  83. MacHIDManager::MacHIDManager()
  84. {
  85. }
  86. //--------------------------------------------------------------------------------//
  87. MacHIDManager::~MacHIDManager()
  88. {
  89. }
  90. //------------------------------------------------------------------------------------------------------//
  91. void MacHIDManager::initialize()
  92. {
  93. //Make the search more specific by adding usage flags
  94. int usage = kHIDUsage_GD_Joystick;
  95. int page = kHIDPage_GenericDesktop;
  96. io_iterator_t iterator = lookUpDevices(usage, page);
  97. if(iterator)
  98. iterateAndOpenDevices(iterator);
  99. //Doesn't support multiple usage flags, iterate twice
  100. usage = kHIDUsage_GD_GamePad;
  101. iterator = lookUpDevices(usage, page);
  102. if(iterator)
  103. iterateAndOpenDevices(iterator);
  104. }
  105. //------------------------------------------------------------------------------------------------------//
  106. io_iterator_t MacHIDManager::lookUpDevices(int usage, int page)
  107. {
  108. CFMutableDictionaryRef deviceLookupMap = IOServiceMatching(kIOHIDDeviceKey);
  109. if(!deviceLookupMap)
  110. OIS_EXCEPT(E_General, "Could not setup HID device search parameters");
  111. CFNumberRef usageRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
  112. CFNumberRef pageRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
  113. CFDictionarySetValue(deviceLookupMap, CFSTR(kIOHIDPrimaryUsageKey), usageRef);
  114. CFDictionarySetValue(deviceLookupMap, CFSTR(kIOHIDPrimaryUsagePageKey), pageRef);
  115. //IOServiceGetMatchingServices consumes the map so we do not have to release it ourself
  116. io_iterator_t iterator = 0;
  117. IOReturn result = IOServiceGetMatchingServices(kIOMasterPortDefault, deviceLookupMap, &iterator);
  118. CFRelease(usageRef);
  119. CFRelease(pageRef);
  120. if(result == kIOReturnSuccess)
  121. {
  122. return iterator;
  123. }
  124. //TODO: Throw exception instead?
  125. else
  126. {
  127. return 0;
  128. }
  129. }
  130. //------------------------------------------------------------------------------------------------------//
  131. void MacHIDManager::iterateAndOpenDevices(io_iterator_t iterator)
  132. {
  133. io_object_t hidDevice = 0;
  134. while ((hidDevice = IOIteratorNext(iterator)) !=0)
  135. {
  136. //Get the current registry items property map
  137. CFMutableDictionaryRef propertyMap = 0;
  138. if (IORegistryEntryCreateCFProperties(hidDevice, &propertyMap, kCFAllocatorDefault, kNilOptions) == KERN_SUCCESS && propertyMap)
  139. {
  140. //Go through device to find all needed info
  141. HidInfo* hid = enumerateDeviceProperties(propertyMap);
  142. if(hid)
  143. {
  144. //todo - we need to hold an open interface so we do not have to enumerate again later
  145. //should be able to watch for device removals also
  146. // Testing opening / closing interface
  147. IOCFPlugInInterface **pluginInterface = NULL;
  148. SInt32 score = 0;
  149. if (IOCreatePlugInInterfaceForService(hidDevice, kIOHIDDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &pluginInterface, &score) == kIOReturnSuccess)
  150. {
  151. IOHIDDeviceInterface **interface;
  152. HRESULT pluginResult = (*pluginInterface)->QueryInterface(pluginInterface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (void **)&(interface));
  153. if(pluginResult != S_OK)
  154. OIS_EXCEPT(E_General, "Not able to create plugin interface");
  155. IODestroyPlugInInterface(pluginInterface);
  156. hid->interface = interface;
  157. //Check for duplicates - some devices have multiple usage
  158. if(std::find(mDeviceList.begin(), mDeviceList.end(), hid) == mDeviceList.end())
  159. mDeviceList.push_back(hid);
  160. }
  161. }
  162. CFRelease(propertyMap);
  163. }
  164. }
  165. IOObjectRelease(iterator);
  166. }
  167. //------------------------------------------------------------------------------------------------------//
  168. HidInfo* MacHIDManager::enumerateDeviceProperties(CFMutableDictionaryRef propertyMap)
  169. {
  170. HidInfo* info = new HidInfo();
  171. info->type = OISJoyStick;
  172. CFStringRef str = getDictionaryItemAsRef<CFStringRef>(propertyMap, kIOHIDManufacturerKey);
  173. if (str)
  174. info->vendor = CFStringGetCStringPtr(str, CFStringGetSystemEncoding());
  175. str = getDictionaryItemAsRef<CFStringRef>(propertyMap, kIOHIDProductKey);
  176. if (str)
  177. info->productKey = CFStringGetCStringPtr(str, CFStringGetSystemEncoding());
  178. info->combinedKey = info->vendor + " " + info->productKey;
  179. //Go through all items in this device (i.e. buttons, hats, sticks, axes, etc)
  180. CFArrayRef array = getDictionaryItemAsRef<CFArrayRef>(propertyMap, kIOHIDElementKey);
  181. if (array)
  182. for (int i = 0; i < CFArrayGetCount(array); i++)
  183. parseDeviceProperties(getArrayItemAsRef<CFDictionaryRef>(array, i));
  184. return info;
  185. }
  186. //------------------------------------------------------------------------------------------------------//
  187. void MacHIDManager::parseDeviceProperties(CFDictionaryRef properties)
  188. {
  189. if(!properties)
  190. return;
  191. CFArrayRef array = getDictionaryItemAsRef<CFArrayRef>(properties, kIOHIDElementKey);
  192. if (array)
  193. {
  194. for (int i = 0; i < CFArrayGetCount(array); i++)
  195. {
  196. CFDictionaryRef element = getArrayItemAsRef<CFDictionaryRef>(array, i);
  197. if (element)
  198. {
  199. if(getInt32(getDictionaryItemAsRef<CFNumberRef>(element, kIOHIDElementTypeKey)) == kIOHIDElementTypeCollection)
  200. { //Check if we need to recurse further intoi another collection
  201. if(getInt32(getDictionaryItemAsRef<CFNumberRef>(element, kIOHIDElementUsagePageKey)) == kHIDPage_GenericDesktop)
  202. parseDeviceProperties(element);
  203. }
  204. else
  205. {
  206. switch(getInt32(getDictionaryItemAsRef<CFNumberRef>(element, kIOHIDElementUsagePageKey)))
  207. {
  208. case kHIDPage_GenericDesktop:
  209. switch(getInt32(getDictionaryItemAsRef<CFNumberRef>(element, kIOHIDElementUsageKey)))
  210. {
  211. case kHIDUsage_GD_Pointer:
  212. cout << "\tkHIDUsage_GD_Pointer\n";
  213. parseDevicePropertiesGroup(element);
  214. break;
  215. case kHIDUsage_GD_X:
  216. case kHIDUsage_GD_Y:
  217. case kHIDUsage_GD_Z:
  218. case kHIDUsage_GD_Rx:
  219. case kHIDUsage_GD_Ry:
  220. case kHIDUsage_GD_Rz:
  221. cout << "\tAxis\n";
  222. break;
  223. case kHIDUsage_GD_Slider:
  224. case kHIDUsage_GD_Dial:
  225. case kHIDUsage_GD_Wheel:
  226. cout << "\tUnsupported kHIDUsage_GD_Wheel\n";
  227. break;
  228. case kHIDUsage_GD_Hatswitch:
  229. cout << "\tUnsupported - kHIDUsage_GD_Hatswitch\n";
  230. break;
  231. }
  232. break;
  233. case kHIDPage_Button:
  234. cout << "\tkHIDPage_Button\n";
  235. break;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. //------------------------------------------------------------------------------------------------------//
  243. void MacHIDManager::parseDevicePropertiesGroup(CFDictionaryRef properties)
  244. {
  245. if(!properties)
  246. return;
  247. CFArrayRef array = getDictionaryItemAsRef<CFArrayRef>(properties, kIOHIDElementKey);
  248. if(array)
  249. {
  250. for (int i = 0; i < CFArrayGetCount(array); i++)
  251. {
  252. CFDictionaryRef element = getArrayItemAsRef<CFDictionaryRef>(array, i);
  253. if (element)
  254. {
  255. switch(getInt32(getDictionaryItemAsRef<CFNumberRef>(element, kIOHIDElementUsagePageKey)))
  256. {
  257. case kHIDPage_GenericDesktop:
  258. switch(getInt32(getDictionaryItemAsRef<CFNumberRef>(element, kIOHIDElementUsageKey)))
  259. {
  260. case kHIDUsage_GD_X:
  261. case kHIDUsage_GD_Y:
  262. case kHIDUsage_GD_Z:
  263. case kHIDUsage_GD_Rx:
  264. case kHIDUsage_GD_Ry:
  265. case kHIDUsage_GD_Rz:
  266. cout << "\t\tAxis\n";
  267. break;
  268. case kHIDUsage_GD_Slider:
  269. case kHIDUsage_GD_Dial:
  270. case kHIDUsage_GD_Wheel:
  271. cout << "\tUnsupported - kHIDUsage_GD_Wheel\n";
  272. break;
  273. case kHIDUsage_GD_Hatswitch:
  274. cout << "\tUnsupported - kHIDUsage_GD_Hatswitch\n";
  275. break;
  276. }
  277. break;
  278. case kHIDPage_Button:
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. }
  285. //--------------------------------------------------------------------------------//
  286. DeviceList MacHIDManager::freeDeviceList()
  287. {
  288. DeviceList ret;
  289. HidInfoList::iterator it = mDeviceList.begin(), end = mDeviceList.end();
  290. for(; it != end; ++it)
  291. {
  292. if((*it)->inUse == false)
  293. ret.insert(std::make_pair((*it)->type, (*it)->combinedKey));
  294. }
  295. return ret;
  296. }
  297. //--------------------------------------------------------------------------------//
  298. int MacHIDManager::totalDevices(Type iType)
  299. {
  300. int ret = 0;
  301. HidInfoList::iterator it = mDeviceList.begin(), end = mDeviceList.end();
  302. for(; it != end; ++it)
  303. {
  304. if((*it)->type == iType)
  305. ret++;
  306. }
  307. return ret;
  308. }
  309. //--------------------------------------------------------------------------------//
  310. int MacHIDManager::freeDevices(Type iType)
  311. {
  312. int ret = 0;
  313. HidInfoList::iterator it = mDeviceList.begin(), end = mDeviceList.end();
  314. for(; it != end; ++it)
  315. {
  316. if((*it)->inUse == false && (*it)->type == iType)
  317. ret++;
  318. }
  319. return ret;
  320. }
  321. //--------------------------------------------------------------------------------//
  322. bool MacHIDManager::vendorExist(Type iType, const std::string & vendor)
  323. {
  324. HidInfoList::iterator it = mDeviceList.begin(), end = mDeviceList.end();
  325. for(; it != end; ++it)
  326. {
  327. if((*it)->type == iType && (*it)->combinedKey == vendor)
  328. return true;
  329. }
  330. return false;
  331. }
  332. //--------------------------------------------------------------------------------//
  333. Object* MacHIDManager::createObject(InputManager* creator, Type iType, bool bufferMode,
  334. const std::string & vendor)
  335. {
  336. Object *obj = 0;
  337. HidInfoList::iterator it = mDeviceList.begin(), end = mDeviceList.end();
  338. for(; it != end; ++it)
  339. {
  340. if((*it)->inUse == false && (*it)->type == iType && (vendor == "" || (*it)->combinedKey == vendor))
  341. {
  342. switch(iType)
  343. {
  344. case OISJoyStick:
  345. {
  346. int totalDevs = totalDevices(iType);
  347. int freeDevs = freeDevices(iType);
  348. int devID = totalDevs - freeDevs;
  349. obj = new MacJoyStick((*it)->combinedKey, bufferMode, *it, creator, devID);
  350. (*it)->inUse = true;
  351. return obj;
  352. }
  353. case OISTablet:
  354. //Create MacTablet
  355. break;
  356. default:
  357. break;
  358. }
  359. }
  360. }
  361. return obj;
  362. }
  363. //--------------------------------------------------------------------------------//
  364. void MacHIDManager::destroyObject(Object* obj)
  365. {
  366. delete obj;
  367. }