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.

277 lines
8.3KB

  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. {
  39. #if defined (MAC_OS_VERSION_12_0) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_12_0
  40. if (@available (macOS 12.0, *))
  41. return kIOMainPortDefault;
  42. #endif
  43. return kIOMasterPortDefault;
  44. }();
  45. if (IOServiceGetMatchingServices (defaultPort, dict, &iter) == kIOReturnSuccess
  46. && iter != 0)
  47. {
  48. iod = IOIteratorNext (iter);
  49. }
  50. IOObjectRelease (iter);
  51. return iod;
  52. }
  53. bool createAppleRemoteInterface (io_object_t iod, void** device)
  54. {
  55. jassert (*device == nullptr);
  56. io_name_t classname;
  57. if (IOObjectGetClass (iod, classname) == kIOReturnSuccess)
  58. {
  59. IOCFPlugInInterface** cfPlugInInterface = nullptr;
  60. SInt32 score = 0;
  61. if (IOCreatePlugInInterfaceForService (iod,
  62. kIOHIDDeviceUserClientTypeID,
  63. kIOCFPlugInInterfaceID,
  64. &cfPlugInInterface,
  65. &score) == kIOReturnSuccess)
  66. {
  67. HRESULT hr = (*cfPlugInInterface)->QueryInterface (cfPlugInInterface,
  68. CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID),
  69. device);
  70. ignoreUnused (hr);
  71. (*cfPlugInInterface)->Release (cfPlugInInterface);
  72. }
  73. }
  74. return *device != nullptr;
  75. }
  76. void appleRemoteQueueCallback (void* const target, const IOReturn result, void*, void*)
  77. {
  78. if (result == kIOReturnSuccess)
  79. ((AppleRemoteDevice*) target)->handleCallbackInternal();
  80. }
  81. }
  82. bool AppleRemoteDevice::start (const bool inExclusiveMode)
  83. {
  84. if (queue != nullptr)
  85. return true;
  86. stop();
  87. bool result = false;
  88. io_object_t iod = getAppleRemoteDevice();
  89. if (iod != 0)
  90. {
  91. if (createAppleRemoteInterface (iod, &device) && open (inExclusiveMode))
  92. result = true;
  93. else
  94. stop();
  95. IOObjectRelease (iod);
  96. }
  97. return result;
  98. }
  99. void AppleRemoteDevice::stop()
  100. {
  101. if (queue != nullptr)
  102. {
  103. (*(IOHIDQueueInterface**) queue)->stop ((IOHIDQueueInterface**) queue);
  104. (*(IOHIDQueueInterface**) queue)->dispose ((IOHIDQueueInterface**) queue);
  105. (*(IOHIDQueueInterface**) queue)->Release ((IOHIDQueueInterface**) queue);
  106. queue = nullptr;
  107. }
  108. if (device != nullptr)
  109. {
  110. (*(IOHIDDeviceInterface**) device)->close ((IOHIDDeviceInterface**) device);
  111. (*(IOHIDDeviceInterface**) device)->Release ((IOHIDDeviceInterface**) device);
  112. device = nullptr;
  113. }
  114. }
  115. bool AppleRemoteDevice::isActive() const
  116. {
  117. return queue != nullptr;
  118. }
  119. bool AppleRemoteDevice::open (const bool openInExclusiveMode)
  120. {
  121. Array<int> cookies;
  122. CFObjectHolder<CFArrayRef> elements;
  123. auto device122 = (IOHIDDeviceInterface122**) device;
  124. if ((*device122)->copyMatchingElements (device122, nullptr, &elements.object) != kIOReturnSuccess)
  125. return false;
  126. for (int i = 0; i < CFArrayGetCount (elements.object); ++i)
  127. {
  128. auto element = (CFDictionaryRef) CFArrayGetValueAtIndex (elements.object, i);
  129. // get the cookie
  130. CFTypeRef object = CFDictionaryGetValue (element, CFSTR (kIOHIDElementCookieKey));
  131. if (object == nullptr || CFGetTypeID (object) != CFNumberGetTypeID())
  132. continue;
  133. long number;
  134. if (! CFNumberGetValue ((CFNumberRef) object, kCFNumberLongType, &number))
  135. continue;
  136. cookies.add ((int) number);
  137. }
  138. if ((*(IOHIDDeviceInterface**) device)
  139. ->open ((IOHIDDeviceInterface**) device,
  140. openInExclusiveMode ? kIOHIDOptionsTypeSeizeDevice
  141. : kIOHIDOptionsTypeNone) == KERN_SUCCESS)
  142. {
  143. queue = (*(IOHIDDeviceInterface**) device)->allocQueue ((IOHIDDeviceInterface**) device);
  144. if (queue != nullptr)
  145. {
  146. (*(IOHIDQueueInterface**) queue)->create ((IOHIDQueueInterface**) queue, 0, 12);
  147. for (int i = 0; i < cookies.size(); ++i)
  148. {
  149. IOHIDElementCookie cookie = (IOHIDElementCookie) cookies.getUnchecked(i);
  150. (*(IOHIDQueueInterface**) queue)->addElement ((IOHIDQueueInterface**) queue, cookie, 0);
  151. }
  152. CFRunLoopSourceRef eventSource;
  153. if ((*(IOHIDQueueInterface**) queue)
  154. ->createAsyncEventSource ((IOHIDQueueInterface**) queue, &eventSource) == KERN_SUCCESS)
  155. {
  156. if ((*(IOHIDQueueInterface**) queue)->setEventCallout ((IOHIDQueueInterface**) queue,
  157. appleRemoteQueueCallback, this, nullptr) == KERN_SUCCESS)
  158. {
  159. CFRunLoopAddSource (CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
  160. (*(IOHIDQueueInterface**) queue)->start ((IOHIDQueueInterface**) queue);
  161. return true;
  162. }
  163. }
  164. }
  165. }
  166. return false;
  167. }
  168. void AppleRemoteDevice::handleCallbackInternal()
  169. {
  170. int totalValues = 0;
  171. AbsoluteTime nullTime = { 0, 0 };
  172. char cookies [12];
  173. int numCookies = 0;
  174. while (numCookies < numElementsInArray (cookies))
  175. {
  176. IOHIDEventStruct e;
  177. if ((*(IOHIDQueueInterface**) queue)->getNextEvent ((IOHIDQueueInterface**) queue, &e, nullTime, 0) != kIOReturnSuccess)
  178. break;
  179. if ((int) e.elementCookie == 19)
  180. {
  181. remoteId = e.value;
  182. buttonPressed (switched, false);
  183. }
  184. else
  185. {
  186. totalValues += e.value;
  187. cookies [numCookies++] = (char) (pointer_sized_int) e.elementCookie;
  188. }
  189. }
  190. cookies [numCookies++] = 0;
  191. static const char buttonPatterns[] =
  192. {
  193. 0x1f, 0x14, 0x12, 0x1f, 0x14, 0x12, 0,
  194. 0x1f, 0x15, 0x12, 0x1f, 0x15, 0x12, 0,
  195. 0x1f, 0x1d, 0x1c, 0x12, 0,
  196. 0x1f, 0x1e, 0x1c, 0x12, 0,
  197. 0x1f, 0x16, 0x12, 0x1f, 0x16, 0x12, 0,
  198. 0x1f, 0x17, 0x12, 0x1f, 0x17, 0x12, 0,
  199. 0x1f, 0x12, 0x04, 0x02, 0,
  200. 0x1f, 0x12, 0x03, 0x02, 0,
  201. 0x1f, 0x12, 0x1f, 0x12, 0,
  202. 0x23, 0x1f, 0x12, 0x23, 0x1f, 0x12, 0,
  203. 19, 0
  204. };
  205. int buttonNum = (int) menuButton;
  206. int i = 0;
  207. while (i < numElementsInArray (buttonPatterns))
  208. {
  209. if (strcmp (cookies, buttonPatterns + i) == 0)
  210. {
  211. buttonPressed ((ButtonType) buttonNum, totalValues > 0);
  212. break;
  213. }
  214. i += (int) strlen (buttonPatterns + i) + 1;
  215. ++buttonNum;
  216. }
  217. }
  218. } // namespace juce