Audio plugin host https://kx.studio/carla
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.

272 lines
8.1KB

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