You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /***************************************************************************
  2. * Copyright (C) 2014 M Wellings *
  3. * info@openforeveryone.co.uk *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License version 2 as *
  7. * published by the Free Software Foundation *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include "qgamecontroller.h"
  20. #include "qgamecontroller_mac_p.h"
  21. #include <QDebug>
  22. IOHIDManagerRef hidManager=NULL;
  23. QGameControllerPrivate::QGameControllerPrivate(uint id, QGameController *q) :
  24. q_ptr(q)
  25. {
  26. ID=id;
  27. Valid=false;
  28. Axis=0;
  29. Buttons=0;
  30. // qDebug("QGameController::QGameController(%i)", ID);
  31. // Create HID Manager reference
  32. if (hidManager==NULL)
  33. {
  34. hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
  35. if (CFGetTypeID(hidManager) == IOHIDManagerGetTypeID()) {
  36. // this is a HID Manager reference!
  37. // qDebug() <<"HID Manager Created";
  38. }else return;
  39. // Create a matching dictionary
  40. CFDictionaryRef matchingCFDictRef = hu_CreateDeviceMatchingDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick);
  41. if (matchingCFDictRef) {
  42. //Set the HID device matching dictionary.
  43. IOHIDManagerSetDeviceMatching(hidManager, matchingCFDictRef);
  44. } else
  45. fprintf(stderr, "%s: hu_CreateDeviceMatchingDictionary failed.", __PRETTY_FUNCTION__);
  46. IOReturn tIOReturn = IOHIDManagerOpen(hidManager, kIOHIDOptionsTypeNone);
  47. if(tIOReturn==kIOReturnSuccess){
  48. // qDebug() <<"IOHIDManagerOpen Success";
  49. }
  50. else return;
  51. CFRelease(matchingCFDictRef);
  52. }
  53. CFSetRef tCFSetRef = IOHIDManagerCopyDevices(hidManager);
  54. if (tCFSetRef==NULL) {
  55. // qDebug() << "No Joysticks Attached";
  56. return;
  57. }
  58. CFIndex count = CFSetGetCount(tCFSetRef);
  59. // qDebug() << count << "Joysticks" << ID;
  60. if (count<=(CFIndex)ID)
  61. return;
  62. const void *values[count];
  63. CFSetGetValues(tCFSetRef, (const void**)values);
  64. if (CFGetTypeID(values[ID]) == IOHIDDeviceGetTypeID()) {
  65. // qDebug() <<"Device found";
  66. device = (IOHIDDeviceRef)values[ID];
  67. CFTypeRef tCFTypeRef = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDManufacturerKey));
  68. if (tCFTypeRef && CFStringGetTypeID() == CFGetTypeID(tCFTypeRef))
  69. {
  70. QString Property=CFStringRefToQString((CFStringRef)tCFTypeRef);
  71. // qDebug() <<"Manufacturer" << Property;
  72. Description.append(Property).append(" ");
  73. }
  74. CFTypeRef tCFTypeRef1 = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));
  75. if (tCFTypeRef1 && CFStringGetTypeID() == CFGetTypeID(tCFTypeRef1))
  76. {
  77. QString Property=CFStringRefToQString((CFStringRef)tCFTypeRef1);
  78. // qDebug() <<"Product" << Property;
  79. Description.append(Property);
  80. }
  81. }else return;
  82. CFArrayRef elementCFArrayRef = IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone);
  83. CFIndex elementcount = CFArrayGetCount(elementCFArrayRef);
  84. // qDebug() << elementcount ;
  85. for (int i = 0; i< elementcount; i++)
  86. {
  87. IOHIDElementRef element= (IOHIDElementRef)CFArrayGetValueAtIndex(elementCFArrayRef, i);
  88. if (CFGetTypeID(element) == IOHIDElementGetTypeID()) {
  89. // qDebug() << "Element found";
  90. IOHIDElementType type = IOHIDElementGetType(element);
  91. if (type==kIOHIDElementTypeInput_Axis || type==kIOHIDElementTypeInput_Misc)
  92. {
  93. int Max = IOHIDElementGetLogicalMax(element);
  94. int Min = IOHIDElementGetLogicalMin(element);
  95. if (IOHIDElementGetUsage(element)!=kHIDUsage_GD_Hatswitch)
  96. {
  97. // qDebug() << "Axis found" << Min << Max;
  98. Axis++;
  99. axisElements.append(element);
  100. axisMaxVals.append(Max);
  101. axisMinVals.append(Min);
  102. }else
  103. {
  104. // qDebug() << "HAT found";
  105. Axis=Axis+2;
  106. axisElements.append(element);
  107. axisElements.append(element);
  108. axisMaxVals.append(Max);
  109. axisMinVals.append(Min);
  110. axisMaxVals.append(Max);
  111. axisMinVals.append(Min);
  112. }
  113. }
  114. else if(type==kIOHIDElementTypeInput_Button)
  115. {
  116. // qDebug() << "Button found";
  117. Buttons++;
  118. buttonElements.append(element);
  119. }
  120. // else
  121. // qDebug() << type<< " found";
  122. }
  123. }
  124. Valid =true;
  125. }
  126. QString CFStringRefToQString(CFStringRef str)
  127. {
  128. if (!str)
  129. return QString();
  130. CFIndex length = CFStringGetLength(str);
  131. if (length == 0)
  132. return QString();
  133. QString string(length, Qt::Uninitialized);
  134. CFStringGetCharacters(str, CFRangeMake(0, length), reinterpret_cast<UniChar *>
  135. (const_cast<QChar *>(string.unicode())));
  136. return string;
  137. }
  138. // function to create matching dictionary
  139. CFMutableDictionaryRef hu_CreateDeviceMatchingDictionary(UInt32 inUsagePage, UInt32 inUsage)
  140. {
  141. // create a dictionary to add usage page/usages to
  142. CFMutableDictionaryRef result = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  143. if (result)
  144. {
  145. if (inUsagePage)
  146. {
  147. // Add key for device type to refine the matching dictionary.
  148. CFNumberRef pageCFNumberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &inUsagePage);
  149. if (pageCFNumberRef)
  150. {
  151. CFDictionarySetValue(result,CFSTR(kIOHIDDeviceUsagePageKey), pageCFNumberRef);
  152. CFRelease(pageCFNumberRef);
  153. // note: the usage is only valid if the usage page is also defined
  154. if (inUsage)
  155. {
  156. CFNumberRef usageCFNumberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &inUsage);
  157. if (usageCFNumberRef)
  158. {
  159. CFDictionarySetValue(result, CFSTR(kIOHIDDeviceUsageKey), usageCFNumberRef);
  160. CFRelease(usageCFNumberRef);
  161. }
  162. else
  163. fprintf(stderr, "%s: CFNumberCreate(usage) failed.", __PRETTY_FUNCTION__);
  164. }
  165. }
  166. else
  167. fprintf(stderr, "%s: CFNumberCreate(usage page) failed.", __PRETTY_FUNCTION__);
  168. }
  169. } else
  170. fprintf(stderr, "%s: CFDictionaryCreateMutable failed.", __PRETTY_FUNCTION__);
  171. return result;
  172. } // hu_CreateDeviceMatchingDictionary
  173. void QGameControllerPrivate::readGameController()
  174. {
  175. Q_Q(QGameController);
  176. IOReturn result;
  177. IOHIDValueRef HIDValue;
  178. for (uint axidID = 0; axidID<Axis; axidID++)
  179. {
  180. Q_ASSERT((int)axidID<axisElements.count());
  181. Q_ASSERT((int)axidID<axisMinVals.count());
  182. Q_ASSERT((int)axidID<axisMaxVals.count());
  183. IOHIDElementRef element = axisElements.at(axidID);
  184. result= IOHIDDeviceGetValue(device, element, &HIDValue);
  185. int min = axisMinVals.at(axidID);
  186. int max = axisMaxVals.at(axidID);
  187. if (CFGetTypeID(HIDValue) == IOHIDValueGetTypeID()) {
  188. int ivalue = IOHIDValueGetIntegerValue(HIDValue);
  189. if (IOHIDElementGetUsage(element)!=kHIDUsage_GD_Hatswitch)
  190. {
  191. //Treat as ordinary axis.
  192. if (ivalue!=AxisValues.value(axidID))
  193. {
  194. AxisValues.insert(axidID,ivalue);
  195. float value = ((float)(ivalue-min)/(float)(max-min) -.5 )*2.0;
  196. // qDebug() << ID << "Axis value" << axidID << "changed to " << ivalue << value;
  197. QGameControllerAxisEvent* event=new QGameControllerAxisEvent(ID, axidID, value);
  198. emit(q->gameControllerAxisEvent((QGameControllerAxisEvent*)event));
  199. }
  200. }else{
  201. //This is a POV hat.
  202. float valueX=0;
  203. float valueY=0;
  204. if (ivalue<min || ivalue>max)
  205. {
  206. // qDebug("POV is centred %i.", ivalue);
  207. }
  208. else
  209. {
  210. float povrads = (float)(ivalue-1)/8*2*3.141593;
  211. // qDebug("POV now %i (%f rads).", ivalue, povrads);
  212. valueX=sin(povrads);
  213. valueY=cos(povrads);
  214. //Dissable these lines to get pov on circle.
  215. valueX=qRound(valueX);
  216. valueY=qRound(valueY);
  217. }
  218. if (valueX!=AxisValues.value(axidID))
  219. {
  220. AxisValues.insert(axidID,valueX);
  221. QGameControllerAxisEvent *event=new QGameControllerAxisEvent(ID, axidID, valueX);
  222. // qDebug("Axis %i moved to %f.", axidID, valueX);
  223. emit(q->gameControllerAxisEvent(event));
  224. }if (valueY!=AxisValues.value(axidID+1))
  225. {
  226. AxisValues.insert(axidID+1,valueY);
  227. QGameControllerAxisEvent *event=new QGameControllerAxisEvent(ID, axidID+1, valueY);
  228. // qDebug("Axis %i moved to %f.", axidID+1, valueY);
  229. emit(q->gameControllerAxisEvent(event));
  230. }
  231. axidID++; //We have dealt with 2 axis in one go.
  232. }
  233. }
  234. }
  235. for (uint i = 0; i<Buttons; i++)
  236. {
  237. Q_ASSERT((int)i<buttonElements.count());
  238. result= IOHIDDeviceGetValue(device, buttonElements.at(i), &HIDValue);
  239. if (CFGetTypeID(HIDValue) == IOHIDValueGetTypeID()) {
  240. bool pressed = IOHIDValueGetIntegerValue(HIDValue);
  241. if (ButtonValues.value(i)!=pressed)
  242. {
  243. // qDebug() << ID << "Button value" << i << "Changed to " << IOHIDValueGetIntegerValue(HIDValue);
  244. ButtonValues.insert(i,pressed);
  245. QGameControllerButtonEvent* event=new QGameControllerButtonEvent(ID, i, pressed);
  246. emit(q->gameControllerButtonEvent((QGameControllerButtonEvent*)event));
  247. }
  248. }
  249. }
  250. }