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.

269 lines
8.0KB

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