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.

262 lines
7.7KB

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