The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

274 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. AppleRemoteDevice::AppleRemoteDevice()
  21. : device (nullptr),
  22. queue (nullptr),
  23. remoteId (0)
  24. {
  25. }
  26. AppleRemoteDevice::~AppleRemoteDevice()
  27. {
  28. stop();
  29. }
  30. namespace
  31. {
  32. io_object_t getAppleRemoteDevice()
  33. {
  34. CFMutableDictionaryRef dict = IOServiceMatching ("AppleIRController");
  35. io_iterator_t iter = 0;
  36. io_object_t iod = 0;
  37. const auto defaultPort =
  38. #if defined (MAC_OS_VERSION_12_0) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_VERSION_12_0
  39. kIOMainPortDefault;
  40. #else
  41. kIOMasterPortDefault;
  42. #endif
  43. if (IOServiceGetMatchingServices (defaultPort, dict, &iter) == kIOReturnSuccess
  44. && iter != 0)
  45. {
  46. iod = IOIteratorNext (iter);
  47. }
  48. IOObjectRelease (iter);
  49. return iod;
  50. }
  51. bool createAppleRemoteInterface (io_object_t iod, void** device)
  52. {
  53. jassert (*device == nullptr);
  54. io_name_t classname;
  55. if (IOObjectGetClass (iod, classname) == kIOReturnSuccess)
  56. {
  57. IOCFPlugInInterface** cfPlugInInterface = nullptr;
  58. SInt32 score = 0;
  59. if (IOCreatePlugInInterfaceForService (iod,
  60. kIOHIDDeviceUserClientTypeID,
  61. kIOCFPlugInInterfaceID,
  62. &cfPlugInInterface,
  63. &score) == kIOReturnSuccess)
  64. {
  65. HRESULT hr = (*cfPlugInInterface)->QueryInterface (cfPlugInInterface,
  66. CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID),
  67. device);
  68. ignoreUnused (hr);
  69. (*cfPlugInInterface)->Release (cfPlugInInterface);
  70. }
  71. }
  72. return *device != nullptr;
  73. }
  74. void appleRemoteQueueCallback (void* const target, const IOReturn result, void*, void*)
  75. {
  76. if (result == kIOReturnSuccess)
  77. ((AppleRemoteDevice*) target)->handleCallbackInternal();
  78. }
  79. }
  80. bool AppleRemoteDevice::start (const bool inExclusiveMode)
  81. {
  82. if (queue != nullptr)
  83. return true;
  84. stop();
  85. bool result = false;
  86. io_object_t iod = getAppleRemoteDevice();
  87. if (iod != 0)
  88. {
  89. if (createAppleRemoteInterface (iod, &device) && open (inExclusiveMode))
  90. result = true;
  91. else
  92. stop();
  93. IOObjectRelease (iod);
  94. }
  95. return result;
  96. }
  97. void AppleRemoteDevice::stop()
  98. {
  99. if (queue != nullptr)
  100. {
  101. (*(IOHIDQueueInterface**) queue)->stop ((IOHIDQueueInterface**) queue);
  102. (*(IOHIDQueueInterface**) queue)->dispose ((IOHIDQueueInterface**) queue);
  103. (*(IOHIDQueueInterface**) queue)->Release ((IOHIDQueueInterface**) queue);
  104. queue = nullptr;
  105. }
  106. if (device != nullptr)
  107. {
  108. (*(IOHIDDeviceInterface**) device)->close ((IOHIDDeviceInterface**) device);
  109. (*(IOHIDDeviceInterface**) device)->Release ((IOHIDDeviceInterface**) device);
  110. device = nullptr;
  111. }
  112. }
  113. bool AppleRemoteDevice::isActive() const
  114. {
  115. return queue != nullptr;
  116. }
  117. bool AppleRemoteDevice::open (const bool openInExclusiveMode)
  118. {
  119. Array<int> cookies;
  120. CFObjectHolder<CFArrayRef> elements;
  121. auto device122 = (IOHIDDeviceInterface122**) device;
  122. if ((*device122)->copyMatchingElements (device122, nullptr, &elements.object) != kIOReturnSuccess)
  123. return false;
  124. for (int i = 0; i < CFArrayGetCount (elements.object); ++i)
  125. {
  126. auto element = (CFDictionaryRef) CFArrayGetValueAtIndex (elements.object, i);
  127. // get the cookie
  128. CFTypeRef object = CFDictionaryGetValue (element, CFSTR (kIOHIDElementCookieKey));
  129. if (object == nullptr || CFGetTypeID (object) != CFNumberGetTypeID())
  130. continue;
  131. long number;
  132. if (! CFNumberGetValue ((CFNumberRef) object, kCFNumberLongType, &number))
  133. continue;
  134. cookies.add ((int) number);
  135. }
  136. if ((*(IOHIDDeviceInterface**) device)
  137. ->open ((IOHIDDeviceInterface**) device,
  138. openInExclusiveMode ? kIOHIDOptionsTypeSeizeDevice
  139. : kIOHIDOptionsTypeNone) == KERN_SUCCESS)
  140. {
  141. queue = (*(IOHIDDeviceInterface**) device)->allocQueue ((IOHIDDeviceInterface**) device);
  142. if (queue != nullptr)
  143. {
  144. (*(IOHIDQueueInterface**) queue)->create ((IOHIDQueueInterface**) queue, 0, 12);
  145. for (int i = 0; i < cookies.size(); ++i)
  146. {
  147. IOHIDElementCookie cookie = (IOHIDElementCookie) cookies.getUnchecked(i);
  148. (*(IOHIDQueueInterface**) queue)->addElement ((IOHIDQueueInterface**) queue, cookie, 0);
  149. }
  150. CFRunLoopSourceRef eventSource;
  151. if ((*(IOHIDQueueInterface**) queue)
  152. ->createAsyncEventSource ((IOHIDQueueInterface**) queue, &eventSource) == KERN_SUCCESS)
  153. {
  154. if ((*(IOHIDQueueInterface**) queue)->setEventCallout ((IOHIDQueueInterface**) queue,
  155. appleRemoteQueueCallback, this, nullptr) == KERN_SUCCESS)
  156. {
  157. CFRunLoopAddSource (CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
  158. (*(IOHIDQueueInterface**) queue)->start ((IOHIDQueueInterface**) queue);
  159. return true;
  160. }
  161. }
  162. }
  163. }
  164. return false;
  165. }
  166. void AppleRemoteDevice::handleCallbackInternal()
  167. {
  168. int totalValues = 0;
  169. AbsoluteTime nullTime = { 0, 0 };
  170. char cookies [12];
  171. int numCookies = 0;
  172. while (numCookies < numElementsInArray (cookies))
  173. {
  174. IOHIDEventStruct e;
  175. if ((*(IOHIDQueueInterface**) queue)->getNextEvent ((IOHIDQueueInterface**) queue, &e, nullTime, 0) != kIOReturnSuccess)
  176. break;
  177. if ((int) e.elementCookie == 19)
  178. {
  179. remoteId = e.value;
  180. buttonPressed (switched, false);
  181. }
  182. else
  183. {
  184. totalValues += e.value;
  185. cookies [numCookies++] = (char) (pointer_sized_int) e.elementCookie;
  186. }
  187. }
  188. cookies [numCookies++] = 0;
  189. static const char buttonPatterns[] =
  190. {
  191. 0x1f, 0x14, 0x12, 0x1f, 0x14, 0x12, 0,
  192. 0x1f, 0x15, 0x12, 0x1f, 0x15, 0x12, 0,
  193. 0x1f, 0x1d, 0x1c, 0x12, 0,
  194. 0x1f, 0x1e, 0x1c, 0x12, 0,
  195. 0x1f, 0x16, 0x12, 0x1f, 0x16, 0x12, 0,
  196. 0x1f, 0x17, 0x12, 0x1f, 0x17, 0x12, 0,
  197. 0x1f, 0x12, 0x04, 0x02, 0,
  198. 0x1f, 0x12, 0x03, 0x02, 0,
  199. 0x1f, 0x12, 0x1f, 0x12, 0,
  200. 0x23, 0x1f, 0x12, 0x23, 0x1f, 0x12, 0,
  201. 19, 0
  202. };
  203. int buttonNum = (int) menuButton;
  204. int i = 0;
  205. while (i < numElementsInArray (buttonPatterns))
  206. {
  207. if (strcmp (cookies, buttonPatterns + i) == 0)
  208. {
  209. buttonPressed ((ButtonType) buttonNum, totalValues > 0);
  210. break;
  211. }
  212. i += (int) strlen (buttonPatterns + i) + 1;
  213. ++buttonNum;
  214. }
  215. }
  216. } // namespace juce