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.

juce_mac_AppleRemote.mm 8.4KB

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