HIDRemote.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. //
  2. // HIDRemote.h
  3. // HIDRemote V1.4 (18th February 2015)
  4. //
  5. // Created by Felix Schwarz on 06.04.07.
  6. // Copyright 2007-2015 IOSPIRIT GmbH. All rights reserved.
  7. //
  8. // The latest version of this class is available at
  9. // http://www.iospirit.com/developers/hidremote/
  10. //
  11. // ** LICENSE *************************************************************************
  12. //
  13. // Copyright (c) 2007-2014 IOSPIRIT GmbH (http://www.iospirit.com/)
  14. // All rights reserved.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistributions of source code must retain the above copyright notice, this list
  20. // of conditions and the following disclaimer.
  21. //
  22. // * Redistributions in binary form must reproduce the above copyright notice, this
  23. // list of conditions and the following disclaimer in the documentation and/or other
  24. // materials provided with the distribution.
  25. //
  26. // * Neither the name of IOSPIRIT GmbH nor the names of its contributors may be used to
  27. // endorse or promote products derived from this software without specific prior
  28. // written permission.
  29. //
  30. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  31. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  33. // SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  34. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  35. // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  36. // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  37. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  39. // DAMAGE.
  40. //
  41. // ************************************************************************************
  42. // ************************************************************************************
  43. // ********************************** DOCUMENTATION ***********************************
  44. // ************************************************************************************
  45. //
  46. // - a reference is available at http://www.iospirit.com/developers/hidremote/reference/
  47. // - for a guide, please see http://www.iospirit.com/developers/hidremote/guide/
  48. //
  49. // ************************************************************************************
  50. #import <Cocoa/Cocoa.h>
  51. // For legacy SDKs
  52. #ifndef MAC_OS_X_VERSION_10_9
  53. #define MAC_OS_X_VERSION_10_9 1090
  54. #endif /* MAC_OS_X_VERSION_10_9 */
  55. #ifndef MAC_OS_X_VERSION_10_10
  56. #define MAC_OS_X_VERSION_10_10 101000
  57. #endif /* MAC_OS_X_VERSION_10_10 */
  58. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10
  59. // Carbon is only required on OS X versions prior to 10.10 (for getting the OS version via Gestalt() -
  60. // replaced by [[NSProcessInfo processInfo] operatingSystemVersion] in 10.10)
  61. #include <Carbon/Carbon.h>
  62. #endif
  63. #include <unistd.h>
  64. #include <mach/mach.h>
  65. #include <sys/types.h>
  66. #include <IOKit/IOKitLib.h>
  67. #include <IOKit/IOCFPlugIn.h>
  68. #include <IOKit/IOMessage.h>
  69. #include <IOKit/hid/IOHIDKeys.h>
  70. #include <IOKit/hid/IOHIDLib.h>
  71. #include <IOKit/hid/IOHIDUsageTables.h>
  72. #include <IOKit/hidsystem/IOHIDLib.h>
  73. #include <IOKit/hidsystem/IOHIDParameter.h>
  74. #include <IOKit/hidsystem/IOHIDShared.h>
  75. #pragma mark -- Enums / Codes --
  76. typedef enum
  77. {
  78. kHIDRemoteModeNone = 0L,
  79. kHIDRemoteModeShared, // Share the remote with others - let's you listen to the remote control events as long as noone has an exclusive lock on it
  80. // (RECOMMENDED ONLY FOR SPECIAL PURPOSES)
  81. kHIDRemoteModeExclusive, // Try to acquire an exclusive lock on the remote (NOT RECOMMENDED)
  82. kHIDRemoteModeExclusiveAuto // Try to acquire an exclusive lock on the remote whenever the application has focus. Temporarily release control over the
  83. // remote when another application has focus (RECOMMENDED)
  84. } HIDRemoteMode;
  85. typedef enum
  86. {
  87. /* A code reserved for "no button" (needed for tracking) */
  88. kHIDRemoteButtonCodeNone = 0L,
  89. /* Standard codes - available for white plastic and aluminum remote */
  90. kHIDRemoteButtonCodeUp,
  91. kHIDRemoteButtonCodeDown,
  92. kHIDRemoteButtonCodeLeft,
  93. kHIDRemoteButtonCodeRight,
  94. kHIDRemoteButtonCodeCenter,
  95. kHIDRemoteButtonCodeMenu,
  96. /* Extra codes - Only available for the new aluminum version of the remote */
  97. kHIDRemoteButtonCodePlay,
  98. /* Masks */
  99. kHIDRemoteButtonCodeCodeMask = 0xFFL,
  100. kHIDRemoteButtonCodeHoldMask = (1L << 16L),
  101. kHIDRemoteButtonCodeSpecialMask = (1L << 17L),
  102. kHIDRemoteButtonCodeAluminumMask = (1L << 21L), // PRIVATE - only used internally
  103. /* Hold button standard codes - available for white plastic and aluminum remote */
  104. kHIDRemoteButtonCodeUpHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeUp),
  105. kHIDRemoteButtonCodeDownHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeDown),
  106. kHIDRemoteButtonCodeLeftHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeLeft),
  107. kHIDRemoteButtonCodeRightHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeRight),
  108. kHIDRemoteButtonCodeCenterHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeCenter),
  109. kHIDRemoteButtonCodeMenuHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeMenu),
  110. /* Hold button extra codes - Only available for aluminum version of the remote */
  111. kHIDRemoteButtonCodePlayHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodePlay),
  112. /* DEPRECATED codes - compatibility with HIDRemote 1.0 */
  113. kHIDRemoteButtonCodePlus = kHIDRemoteButtonCodeUp,
  114. kHIDRemoteButtonCodePlusHold = kHIDRemoteButtonCodeUpHold,
  115. kHIDRemoteButtonCodeMinus = kHIDRemoteButtonCodeDown,
  116. kHIDRemoteButtonCodeMinusHold = kHIDRemoteButtonCodeDownHold,
  117. kHIDRemoteButtonCodePlayPause = kHIDRemoteButtonCodeCenter,
  118. kHIDRemoteButtonCodePlayPauseHold = kHIDRemoteButtonCodeCenterHold,
  119. /* Special purpose codes */
  120. kHIDRemoteButtonCodeIDChanged = (kHIDRemoteButtonCodeSpecialMask|(1L << 18L)), // (the ID of the connected remote has changed, you can safely ignore this)
  121. #ifdef _HIDREMOTE_EXTENSIONS
  122. #define _HIDREMOTE_EXTENSIONS_SECTION 1
  123. #include "HIDRemoteAdditions.h"
  124. #undef _HIDREMOTE_EXTENSIONS_SECTION
  125. #endif /* _HIDREMOTE_EXTENSIONS */
  126. } HIDRemoteButtonCode;
  127. typedef enum
  128. {
  129. kHIDRemoteModelUndetermined = 0L, // Assume a white plastic remote
  130. kHIDRemoteModelWhitePlastic, // Signal *likely* to be coming from a white plastic remote
  131. kHIDRemoteModelAluminum // Signal *definitely* coming from an aluminum remote
  132. } HIDRemoteModel;
  133. typedef enum
  134. {
  135. kHIDRemoteAluminumRemoteSupportLevelNone = 0L, // This system has no support for the Aluminum Remote at all
  136. kHIDRemoteAluminumRemoteSupportLevelEmulation, // This system possibly has support for the Aluminum Remote (via emulation)
  137. kHIDRemoteAluminumRemoteSupportLevelNative // This system has native support for the Aluminum Remote
  138. } HIDRemoteAluminumRemoteSupportLevel;
  139. @class HIDRemote;
  140. #pragma mark -- Delegate protocol (mandatory) --
  141. @protocol HIDRemoteDelegate
  142. // Notification of button events
  143. - (void)hidRemote:(HIDRemote *)hidRemote // The instance of HIDRemote sending this
  144. eventWithButton:(HIDRemoteButtonCode)buttonCode // Event for the button specified by code
  145. isPressed:(BOOL)isPressed // The button was pressed (YES) / released (NO)
  146. fromHardwareWithAttributes:(NSMutableDictionary *)attributes; // Information on the device this event comes from
  147. @optional
  148. // Notification of ID changes
  149. - (void)hidRemote:(HIDRemote *)hidRemote // Invoked when the user switched to a remote control with a different ID
  150. remoteIDChangedOldID:(SInt32)old
  151. newID:(SInt32)newID
  152. forHardwareWithAttributes:(NSMutableDictionary *)attributes;
  153. // Notification about hardware additions/removals
  154. - (void)hidRemote:(HIDRemote *)hidRemote // Invoked when new hardware was found / added to HIDRemote's pool
  155. foundNewHardwareWithAttributes:(NSMutableDictionary *)attributes;
  156. - (void)hidRemote:(HIDRemote *)hidRemote // Invoked when initialization of new hardware as requested failed
  157. failedNewHardwareWithError:(NSError *)error;
  158. - (void)hidRemote:(HIDRemote *)hidRemote // Invoked when hardware was removed from HIDRemote's pool
  159. releasedHardwareWithAttributes:(NSMutableDictionary *)attributes;
  160. // ### WARNING: Unless you know VERY PRECISELY what you are doing, do not implement any of the delegate methods below. ###
  161. // Matching of newly found receiver hardware
  162. - (BOOL)hidRemote:(HIDRemote *)hidRemote // Invoked when new hardware is inspected
  163. inspectNewHardwareWithService:(io_service_t)service //
  164. prematchResult:(BOOL)prematchResult; // Return YES if HIDRemote should go on with this hardware and try
  165. // to use it, or NO if it should not be persued further.
  166. // Exlusive lock lending
  167. - (BOOL)hidRemote:(HIDRemote *)hidRemote
  168. lendExclusiveLockToApplicationWithInfo:(NSDictionary *)applicationInfo;
  169. - (void)hidRemote:(HIDRemote *)hidRemote
  170. exclusiveLockReleasedByApplicationWithInfo:(NSDictionary *)applicationInfo;
  171. - (BOOL)hidRemote:(HIDRemote *)hidRemote
  172. shouldRetryExclusiveLockWithInfo:(NSDictionary *)applicationInfo;
  173. @end
  174. #pragma mark -- Actual header file for class --
  175. @interface HIDRemote : NSObject
  176. {
  177. // IOMasterPort
  178. mach_port_t _masterPort;
  179. // Notification ports
  180. IONotificationPortRef _notifyPort;
  181. CFRunLoopSourceRef _notifyRLSource;
  182. // Matching iterator
  183. io_iterator_t _matchingServicesIterator;
  184. // SecureInput notification
  185. io_object_t _secureInputNotification;
  186. // Service attributes
  187. NSMutableDictionary *_serviceAttribMap;
  188. // Mode
  189. HIDRemoteMode _mode;
  190. BOOL _autoRecover;
  191. NSTimer *_autoRecoveryTimer;
  192. // Delegate
  193. NSObject <HIDRemoteDelegate> *_delegate;
  194. // Last seen ID and remote model
  195. SInt32 _lastSeenRemoteID;
  196. HIDRemoteModel _lastSeenModel;
  197. SInt32 _lastSeenModelRemoteID;
  198. // Unused button codes
  199. NSArray *_unusedButtonCodes;
  200. // Simulate Plus/Minus Hold
  201. BOOL _simulateHoldEvents;
  202. // SecureEventInput workaround
  203. BOOL _secureEventInputWorkAround;
  204. UInt64 _lastSecureEventInputPIDSum;
  205. uid_t _lastFrontUserSession;
  206. BOOL _lastScreenIsLocked;
  207. // Exclusive lock lending
  208. BOOL _exclusiveLockLending;
  209. BOOL _sendExclusiveResourceReuseNotification;
  210. NSNumber *_waitForReturnByPID;
  211. NSNumber *_returnToPID;
  212. BOOL _isRestarting;
  213. // Status notifications
  214. BOOL _sendStatusNotifications;
  215. NSString *_pidString;
  216. // Status
  217. BOOL _applicationIsTerminating;
  218. BOOL _isStopping;
  219. // Thread safety
  220. #ifdef HIDREMOTE_THREADSAFETY_HARDENED_NOTIFICATION_HANDLING /* #define HIDREMOTE_THREADSAFETY_HARDENED_NOTIFICATION_HANDLING if you're running your HIDRemote instance on a background thread (requires OS X 10.5 or later) */
  221. NSThread *_runOnThread;
  222. #endif
  223. }
  224. #pragma mark -- PUBLIC: Shared HID Remote --
  225. + (HIDRemote *)sharedHIDRemote;
  226. #pragma mark -- PUBLIC: System Information --
  227. + (BOOL)isCandelairInstalled;
  228. + (BOOL)isCandelairInstallationRequiredForRemoteMode:(HIDRemoteMode)remoteMode;
  229. + (SInt32)OSXVersion;
  230. - (HIDRemoteAluminumRemoteSupportLevel)aluminiumRemoteSystemSupportLevel;
  231. #pragma mark -- PUBLIC: Interface / API --
  232. - (BOOL)startRemoteControl:(HIDRemoteMode)hidRemoteMode;
  233. - (void)stopRemoteControl;
  234. - (BOOL)isStarted;
  235. - (HIDRemoteMode)startedInMode;
  236. - (unsigned)activeRemoteControlCount;
  237. - (SInt32)lastSeenRemoteControlID;
  238. - (void)setLastSeenModel:(HIDRemoteModel)aModel;
  239. - (HIDRemoteModel)lastSeenModel;
  240. - (void)setDelegate:(NSObject <HIDRemoteDelegate> *)newDelegate;
  241. - (NSObject <HIDRemoteDelegate> *)delegate;
  242. - (void)setSimulateHoldEvents:(BOOL)newSimulateHoldEvents;
  243. - (BOOL)simulateHoldEvents;
  244. - (void)setUnusedButtonCodes:(NSArray *)newArrayWithUnusedButtonCodesAsNSNumbers;
  245. - (NSArray *)unusedButtonCodes;
  246. #pragma mark -- PUBLIC: Expert APIs --
  247. - (void)setEnableSecureEventInputWorkaround:(BOOL)newEnableSecureEventInputWorkaround;
  248. - (BOOL)enableSecureEventInputWorkaround;
  249. - (void)setExclusiveLockLendingEnabled:(BOOL)newExclusiveLockLendingEnabled;
  250. - (BOOL)exclusiveLockLendingEnabled;
  251. - (BOOL)isApplicationTerminating;
  252. - (BOOL)isStopping;
  253. #pragma mark -- PRIVATE: HID Event handling --
  254. - (void)_handleButtonCode:(HIDRemoteButtonCode)buttonCode isPressed:(BOOL)isPressed hidAttribsDict:(NSMutableDictionary *)hidAttribsDict;
  255. - (void)_sendButtonCode:(HIDRemoteButtonCode)buttonCode isPressed:(BOOL)isPressed hidAttribsDict:(NSMutableDictionary *)hidAttribsDict;
  256. - (void)_hidEventFor:(io_service_t)hidDevice from:(IOHIDQueueInterface **)interface withResult:(IOReturn)result;
  257. #pragma mark -- PRIVATE: Service setup and destruction --
  258. - (BOOL)_prematchService:(io_object_t)service;
  259. - (HIDRemoteButtonCode)buttonCodeForUsage:(unsigned int)usage usagePage:(unsigned int)usagePage;
  260. - (BOOL)_setupService:(io_object_t)service;
  261. - (void)_destructService:(io_object_t)service;
  262. #pragma mark -- PRIVATE: Distributed notifiations handling --
  263. - (void)_postStatusWithAction:(NSString *)action;
  264. - (void)_handleNotifications:(NSNotification *)notification;
  265. - (void)_setSendStatusNotifications:(BOOL)doSend;
  266. - (BOOL)_sendStatusNotifications;
  267. #pragma mark -- PRIVATE: Application becomes active / inactive handling for kHIDRemoteModeExclusiveAuto --
  268. - (void)_appStatusChanged:(NSNotification *)notification;
  269. - (void)_delayedAutoRecovery:(NSTimer *)aTimer;
  270. #pragma mark -- PRIVATE: Notification handling --
  271. - (void)_serviceMatching:(io_iterator_t)iterator;
  272. - (void)_serviceNotificationFor:(io_service_t)service messageType:(natural_t)messageType messageArgument:(void *)messageArgument;
  273. - (void)_updateSessionInformation;
  274. - (void)_secureInputNotificationFor:(io_service_t)service messageType:(natural_t)messageType messageArgument:(void *)messageArgument;
  275. @end
  276. #pragma mark -- Information attribute keys --
  277. extern NSString *kHIDRemoteManufacturer;
  278. extern NSString *kHIDRemoteProduct;
  279. extern NSString *kHIDRemoteTransport;
  280. #pragma mark -- Internal/Expert attribute keys (AKA: don't touch these unless you really, really, REALLY know what you do) --
  281. extern NSString *kHIDRemoteCFPluginInterface;
  282. extern NSString *kHIDRemoteHIDDeviceInterface;
  283. extern NSString *kHIDRemoteCookieButtonCodeLUT;
  284. extern NSString *kHIDRemoteHIDQueueInterface;
  285. extern NSString *kHIDRemoteServiceNotification;
  286. extern NSString *kHIDRemoteCFRunLoopSource;
  287. extern NSString *kHIDRemoteLastButtonPressed;
  288. extern NSString *kHIDRemoteService;
  289. extern NSString *kHIDRemoteSimulateHoldEventsTimer;
  290. extern NSString *kHIDRemoteSimulateHoldEventsOriginButtonCode;
  291. extern NSString *kHIDRemoteAluminumRemoteSupportLevel;
  292. extern NSString *kHIDRemoteAluminumRemoteSupportOnDemand;
  293. #pragma mark -- Distributed notifications --
  294. extern NSString *kHIDRemoteDNHIDRemotePing;
  295. extern NSString *kHIDRemoteDNHIDRemoteRetry;
  296. extern NSString *kHIDRemoteDNHIDRemoteStatus;
  297. extern NSString *kHIDRemoteDNHIDRemoteRetryGlobalObject;
  298. #pragma mark -- Distributed notifications userInfo keys and values --
  299. extern NSString *kHIDRemoteDNStatusHIDRemoteVersionKey;
  300. extern NSString *kHIDRemoteDNStatusPIDKey;
  301. extern NSString *kHIDRemoteDNStatusModeKey;
  302. extern NSString *kHIDRemoteDNStatusUnusedButtonCodesKey;
  303. extern NSString *kHIDRemoteDNStatusRemoteControlCountKey;
  304. extern NSString *kHIDRemoteDNStatusReturnToPIDKey;
  305. extern NSString *kHIDRemoteDNStatusActionKey;
  306. extern NSString *kHIDRemoteDNStatusActionStart;
  307. extern NSString *kHIDRemoteDNStatusActionStop;
  308. extern NSString *kHIDRemoteDNStatusActionUpdate;
  309. extern NSString *kHIDRemoteDNStatusActionNoNeed;
  310. #pragma mark -- Driver compatibility flags --
  311. typedef enum
  312. {
  313. kHIDRemoteCompatibilityFlagsStandardHIDRemoteDevice = 1L,
  314. } HIDRemoteCompatibilityFlags;