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.4KB

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