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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. AppleRemoteDevice::AppleRemoteDevice()
  18. : device (0),
  19. queue (0),
  20. remoteId (0)
  21. {
  22. }
  23. AppleRemoteDevice::~AppleRemoteDevice()
  24. {
  25. stop();
  26. }
  27. namespace
  28. {
  29. io_object_t getAppleRemoteDevice()
  30. {
  31. CFMutableDictionaryRef dict = IOServiceMatching ("AppleIRController");
  32. io_iterator_t iter = 0;
  33. io_object_t iod = 0;
  34. if (IOServiceGetMatchingServices (kIOMasterPortDefault, dict, &iter) == kIOReturnSuccess
  35. && iter != 0)
  36. {
  37. iod = IOIteratorNext (iter);
  38. }
  39. IOObjectRelease (iter);
  40. return iod;
  41. }
  42. bool createAppleRemoteInterface (io_object_t iod, void** device)
  43. {
  44. jassert (*device == nullptr);
  45. io_name_t classname;
  46. if (IOObjectGetClass (iod, classname) == kIOReturnSuccess)
  47. {
  48. IOCFPlugInInterface** cfPlugInInterface = nullptr;
  49. SInt32 score = 0;
  50. if (IOCreatePlugInInterfaceForService (iod,
  51. kIOHIDDeviceUserClientTypeID,
  52. kIOCFPlugInInterfaceID,
  53. &cfPlugInInterface,
  54. &score) == kIOReturnSuccess)
  55. {
  56. HRESULT hr = (*cfPlugInInterface)->QueryInterface (cfPlugInInterface,
  57. CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID),
  58. device);
  59. (void) hr;
  60. (*cfPlugInInterface)->Release (cfPlugInInterface);
  61. }
  62. }
  63. return *device != 0;
  64. }
  65. void appleRemoteQueueCallback (void* const target, const IOReturn result, void*, void*)
  66. {
  67. if (result == kIOReturnSuccess)
  68. ((AppleRemoteDevice*) target)->handleCallbackInternal();
  69. }
  70. }
  71. bool AppleRemoteDevice::start (const bool inExclusiveMode)
  72. {
  73. if (queue != 0)
  74. return true;
  75. stop();
  76. bool result = false;
  77. io_object_t iod = getAppleRemoteDevice();
  78. if (iod != 0)
  79. {
  80. if (createAppleRemoteInterface (iod, &device) && open (inExclusiveMode))
  81. result = true;
  82. else
  83. stop();
  84. IOObjectRelease (iod);
  85. }
  86. return result;
  87. }
  88. void AppleRemoteDevice::stop()
  89. {
  90. if (queue != 0)
  91. {
  92. (*(IOHIDQueueInterface**) queue)->stop ((IOHIDQueueInterface**) queue);
  93. (*(IOHIDQueueInterface**) queue)->dispose ((IOHIDQueueInterface**) queue);
  94. (*(IOHIDQueueInterface**) queue)->Release ((IOHIDQueueInterface**) queue);
  95. queue = 0;
  96. }
  97. if (device != 0)
  98. {
  99. (*(IOHIDDeviceInterface**) device)->close ((IOHIDDeviceInterface**) device);
  100. (*(IOHIDDeviceInterface**) device)->Release ((IOHIDDeviceInterface**) device);
  101. device = 0;
  102. }
  103. }
  104. bool AppleRemoteDevice::isActive() const
  105. {
  106. return queue != 0;
  107. }
  108. bool AppleRemoteDevice::open (const bool openInExclusiveMode)
  109. {
  110. Array <int> cookies;
  111. CFArrayRef elements;
  112. IOHIDDeviceInterface122** const device122 = (IOHIDDeviceInterface122**) device;
  113. if ((*device122)->copyMatchingElements (device122, 0, &elements) != kIOReturnSuccess)
  114. return false;
  115. for (int i = 0; i < CFArrayGetCount (elements); ++i)
  116. {
  117. CFDictionaryRef element = (CFDictionaryRef) CFArrayGetValueAtIndex (elements, i);
  118. // get the cookie
  119. CFTypeRef object = CFDictionaryGetValue (element, CFSTR (kIOHIDElementCookieKey));
  120. if (object == 0 || CFGetTypeID (object) != CFNumberGetTypeID())
  121. continue;
  122. long number;
  123. if (! CFNumberGetValue ((CFNumberRef) object, kCFNumberLongType, &number))
  124. continue;
  125. cookies.add ((int) number);
  126. }
  127. CFRelease (elements);
  128. if ((*(IOHIDDeviceInterface**) device)
  129. ->open ((IOHIDDeviceInterface**) device,
  130. openInExclusiveMode ? kIOHIDOptionsTypeSeizeDevice
  131. : kIOHIDOptionsTypeNone) == KERN_SUCCESS)
  132. {
  133. queue = (*(IOHIDDeviceInterface**) device)->allocQueue ((IOHIDDeviceInterface**) device);
  134. if (queue != 0)
  135. {
  136. (*(IOHIDQueueInterface**) queue)->create ((IOHIDQueueInterface**) queue, 0, 12);
  137. for (int i = 0; i < cookies.size(); ++i)
  138. {
  139. IOHIDElementCookie cookie = (IOHIDElementCookie) cookies.getUnchecked(i);
  140. (*(IOHIDQueueInterface**) queue)->addElement ((IOHIDQueueInterface**) queue, cookie, 0);
  141. }
  142. CFRunLoopSourceRef eventSource;
  143. if ((*(IOHIDQueueInterface**) queue)
  144. ->createAsyncEventSource ((IOHIDQueueInterface**) queue, &eventSource) == KERN_SUCCESS)
  145. {
  146. if ((*(IOHIDQueueInterface**) queue)->setEventCallout ((IOHIDQueueInterface**) queue,
  147. appleRemoteQueueCallback, this, 0) == KERN_SUCCESS)
  148. {
  149. CFRunLoopAddSource (CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
  150. (*(IOHIDQueueInterface**) queue)->start ((IOHIDQueueInterface**) queue);
  151. return true;
  152. }
  153. }
  154. }
  155. }
  156. return false;
  157. }
  158. void AppleRemoteDevice::handleCallbackInternal()
  159. {
  160. int totalValues = 0;
  161. AbsoluteTime nullTime = { 0, 0 };
  162. char cookies [12];
  163. int numCookies = 0;
  164. while (numCookies < numElementsInArray (cookies))
  165. {
  166. IOHIDEventStruct e;
  167. if ((*(IOHIDQueueInterface**) queue)->getNextEvent ((IOHIDQueueInterface**) queue, &e, nullTime, 0) != kIOReturnSuccess)
  168. break;
  169. if ((int) e.elementCookie == 19)
  170. {
  171. remoteId = e.value;
  172. buttonPressed (switched, false);
  173. }
  174. else
  175. {
  176. totalValues += e.value;
  177. cookies [numCookies++] = (char) (pointer_sized_int) e.elementCookie;
  178. }
  179. }
  180. cookies [numCookies++] = 0;
  181. //DBG (String::toHexString ((uint8*) cookies, numCookies, 1) + " " + String (totalValues));
  182. static const char buttonPatterns[] =
  183. {
  184. 0x1f, 0x14, 0x12, 0x1f, 0x14, 0x12, 0,
  185. 0x1f, 0x15, 0x12, 0x1f, 0x15, 0x12, 0,
  186. 0x1f, 0x1d, 0x1c, 0x12, 0,
  187. 0x1f, 0x1e, 0x1c, 0x12, 0,
  188. 0x1f, 0x16, 0x12, 0x1f, 0x16, 0x12, 0,
  189. 0x1f, 0x17, 0x12, 0x1f, 0x17, 0x12, 0,
  190. 0x1f, 0x12, 0x04, 0x02, 0,
  191. 0x1f, 0x12, 0x03, 0x02, 0,
  192. 0x1f, 0x12, 0x1f, 0x12, 0,
  193. 0x23, 0x1f, 0x12, 0x23, 0x1f, 0x12, 0,
  194. 19, 0
  195. };
  196. int buttonNum = (int) menuButton;
  197. int i = 0;
  198. while (i < numElementsInArray (buttonPatterns))
  199. {
  200. if (strcmp (cookies, buttonPatterns + i) == 0)
  201. {
  202. buttonPressed ((ButtonType) buttonNum, totalValues > 0);
  203. break;
  204. }
  205. i += (int) strlen (buttonPatterns + i) + 1;
  206. ++buttonNum;
  207. }
  208. }