HIDRemote.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. //
  2. // HIDRemote.m
  3. // HIDRemote V1.6 (27th September 2017)
  4. //
  5. // Created by Felix Schwarz on 06.04.07.
  6. // Copyright 2007-2017 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-2017 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. #ifndef HIDREMOTE_THREADSAFETY_HARDENED_NOTIFICATION_HANDLING
  64. #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  65. // Enable thread-safe notification handling by default if deploying to OS X >= 10.5
  66. #define HIDREMOTE_THREADSAFETY_HARDENED_NOTIFICATION_HANDLING 1
  67. #else
  68. #define HIDREMOTE_THREADSAFETY_HARDENED_NOTIFICATION_HANDLING 0
  69. #endif
  70. #endif
  71. #include <unistd.h>
  72. #include <mach/mach.h>
  73. #include <sys/types.h>
  74. #include <IOKit/IOKitLib.h>
  75. #include <IOKit/IOCFPlugIn.h>
  76. #include <IOKit/IOMessage.h>
  77. #include <IOKit/hid/IOHIDKeys.h>
  78. #include <IOKit/hid/IOHIDLib.h>
  79. #include <IOKit/hid/IOHIDUsageTables.h>
  80. #include <IOKit/hidsystem/IOHIDLib.h>
  81. #include <IOKit/hidsystem/IOHIDParameter.h>
  82. #include <IOKit/hidsystem/IOHIDShared.h>
  83. #pragma mark - Enums / Codes
  84. #ifndef HID_REMOTE_MODE_ENUM
  85. #define HID_REMOTE_MODE_ENUM 1
  86. typedef enum
  87. {
  88. kHIDRemoteModeNone = 0L,
  89. 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
  90. // (RECOMMENDED ONLY FOR SPECIAL PURPOSES)
  91. kHIDRemoteModeExclusive, // Try to acquire an exclusive lock on the remote (NOT RECOMMENDED)
  92. kHIDRemoteModeExclusiveAuto // Try to acquire an exclusive lock on the remote whenever the application has focus. Temporarily release control over the
  93. // remote when another application has focus (RECOMMENDED)
  94. } HIDRemoteMode;
  95. #endif /* HID_REMOTE_MODE_ENUM */
  96. typedef enum
  97. {
  98. /* A code reserved for "no button" (needed for tracking) */
  99. kHIDRemoteButtonCodeNone = 0L,
  100. /* Standard codes - available for white plastic and aluminum remote */
  101. kHIDRemoteButtonCodeUp,
  102. kHIDRemoteButtonCodeDown,
  103. kHIDRemoteButtonCodeLeft,
  104. kHIDRemoteButtonCodeRight,
  105. kHIDRemoteButtonCodeCenter,
  106. kHIDRemoteButtonCodeMenu,
  107. /* Extra codes - Only available for the new aluminum version of the remote */
  108. kHIDRemoteButtonCodePlay,
  109. /* Masks */
  110. kHIDRemoteButtonCodeCodeMask = 0xFFL,
  111. kHIDRemoteButtonCodeHoldMask = (1L << 16L),
  112. kHIDRemoteButtonCodeSpecialMask = (1L << 17L),
  113. kHIDRemoteButtonCodeAluminumMask = (1L << 21L), // PRIVATE - only used internally
  114. /* Hold button standard codes - available for white plastic and aluminum remote */
  115. kHIDRemoteButtonCodeUpHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeUp),
  116. kHIDRemoteButtonCodeDownHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeDown),
  117. kHIDRemoteButtonCodeLeftHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeLeft),
  118. kHIDRemoteButtonCodeRightHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeRight),
  119. kHIDRemoteButtonCodeCenterHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeCenter),
  120. kHIDRemoteButtonCodeMenuHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodeMenu),
  121. /* Hold button extra codes - Only available for aluminum version of the remote */
  122. kHIDRemoteButtonCodePlayHold = (kHIDRemoteButtonCodeHoldMask|kHIDRemoteButtonCodePlay),
  123. /* DEPRECATED codes - compatibility with HIDRemote 1.0 */
  124. kHIDRemoteButtonCodePlus = kHIDRemoteButtonCodeUp,
  125. kHIDRemoteButtonCodePlusHold = kHIDRemoteButtonCodeUpHold,
  126. kHIDRemoteButtonCodeMinus = kHIDRemoteButtonCodeDown,
  127. kHIDRemoteButtonCodeMinusHold = kHIDRemoteButtonCodeDownHold,
  128. kHIDRemoteButtonCodePlayPause = kHIDRemoteButtonCodeCenter,
  129. kHIDRemoteButtonCodePlayPauseHold = kHIDRemoteButtonCodeCenterHold,
  130. /* Special purpose codes */
  131. kHIDRemoteButtonCodeIDChanged = (kHIDRemoteButtonCodeSpecialMask|(1L << 18L)), // (the ID of the connected remote has changed, you can safely ignore this)
  132. #ifdef _HIDREMOTE_EXTENSIONS
  133. #define _HIDREMOTE_EXTENSIONS_SECTION 1
  134. #include "HIDRemoteAdditions.h"
  135. #undef _HIDREMOTE_EXTENSIONS_SECTION
  136. #endif /* _HIDREMOTE_EXTENSIONS */
  137. } HIDRemoteButtonCode;
  138. typedef enum
  139. {
  140. kHIDRemoteModelUndetermined = 0L, // Assume a white plastic remote
  141. kHIDRemoteModelWhitePlastic, // Signal *likely* to be coming from a white plastic remote
  142. kHIDRemoteModelAluminum // Signal *definitely* coming from an aluminum remote
  143. } HIDRemoteModel;
  144. typedef enum
  145. {
  146. kHIDRemoteAluminumRemoteSupportLevelNone = 0L, // This system has no support for the Aluminum Remote at all
  147. kHIDRemoteAluminumRemoteSupportLevelEmulation, // This system possibly has support for the Aluminum Remote (via emulation)
  148. kHIDRemoteAluminumRemoteSupportLevelNative // This system has native support for the Aluminum Remote
  149. } HIDRemoteAluminumRemoteSupportLevel;
  150. @class HIDRemote;
  151. #pragma mark - Delegate protocol (mandatory)
  152. @protocol HIDRemoteDelegate
  153. // Notification of button events
  154. - (void)hidRemote:(HIDRemote *)hidRemote // The instance of HIDRemote sending this
  155. eventWithButton:(HIDRemoteButtonCode)buttonCode // Event for the button specified by code
  156. isPressed:(BOOL)isPressed // The button was pressed (YES) / released (NO)
  157. fromHardwareWithAttributes:(NSMutableDictionary *)attributes; // Information on the device this event comes from
  158. @optional
  159. // Notification of ID changes
  160. - (void)hidRemote:(HIDRemote *)hidRemote // Invoked when the user switched to a remote control with a different ID
  161. remoteIDChangedOldID:(SInt32)old
  162. newID:(SInt32)newID
  163. forHardwareWithAttributes:(NSMutableDictionary *)attributes;
  164. // Notification about hardware additions/removals
  165. - (void)hidRemote:(HIDRemote *)hidRemote // Invoked when new hardware was found / added to HIDRemote's pool
  166. foundNewHardwareWithAttributes:(NSMutableDictionary *)attributes;
  167. - (void)hidRemote:(HIDRemote *)hidRemote // Invoked when initialization of new hardware as requested failed
  168. failedNewHardwareWithError:(NSError *)error;
  169. - (void)hidRemote:(HIDRemote *)hidRemote // Invoked when hardware was removed from HIDRemote's pool
  170. releasedHardwareWithAttributes:(NSMutableDictionary *)attributes;
  171. // ### WARNING: Unless you know VERY PRECISELY what you are doing, do not implement any of the delegate methods below. ###
  172. // Matching of newly found receiver hardware
  173. - (BOOL)hidRemote:(HIDRemote *)hidRemote // Invoked when new hardware is inspected
  174. inspectNewHardwareWithService:(io_service_t)service //
  175. prematchResult:(BOOL)prematchResult; // Return YES if HIDRemote should go on with this hardware and try
  176. // to use it, or NO if it should not be persued further.
  177. // Exlusive lock lending
  178. - (BOOL)hidRemote:(HIDRemote *)hidRemote
  179. lendExclusiveLockToApplicationWithInfo:(NSDictionary *)applicationInfo;
  180. - (void)hidRemote:(HIDRemote *)hidRemote
  181. exclusiveLockReleasedByApplicationWithInfo:(NSDictionary *)applicationInfo;
  182. - (BOOL)hidRemote:(HIDRemote *)hidRemote
  183. shouldRetryExclusiveLockWithInfo:(NSDictionary *)applicationInfo;
  184. @end
  185. #pragma mark - Actual header file for class
  186. @interface HIDRemote : NSObject
  187. {
  188. // IOMasterPort
  189. mach_port_t _masterPort;
  190. // Notification ports
  191. IONotificationPortRef _notifyPort;
  192. CFRunLoopSourceRef _notifyRLSource;
  193. // Matching iterator
  194. io_iterator_t _matchingServicesIterator;
  195. // SecureInput notification
  196. io_object_t _secureInputNotification;
  197. // Service attributes
  198. NSMutableDictionary *_serviceAttribMap;
  199. // Mode
  200. HIDRemoteMode _mode;
  201. BOOL _autoRecover;
  202. NSTimer *_autoRecoveryTimer;
  203. // Delegate
  204. NSObject <HIDRemoteDelegate> *_delegate;
  205. // Last seen ID and remote model
  206. SInt32 _lastSeenRemoteID;
  207. HIDRemoteModel _lastSeenModel;
  208. SInt32 _lastSeenModelRemoteID;
  209. // Unused button codes
  210. NSArray *_unusedButtonCodes;
  211. // Simulate Plus/Minus Hold
  212. BOOL _simulateHoldEvents;
  213. // SecureEventInput workaround
  214. BOOL _secureEventInputWorkAround;
  215. UInt64 _lastSecureEventInputPIDSum;
  216. uid_t _lastFrontUserSession;
  217. BOOL _lastScreenIsLocked;
  218. // Exclusive lock lending
  219. BOOL _exclusiveLockLending;
  220. BOOL _sendExclusiveResourceReuseNotification;
  221. NSNumber *_waitForReturnByPID;
  222. NSNumber *_returnToPID;
  223. BOOL _isRestarting;
  224. // Status notifications
  225. BOOL _sendStatusNotifications;
  226. NSString *_pidString;
  227. // Status
  228. BOOL _applicationIsTerminating;
  229. BOOL _isStopping;
  230. // Thread safety
  231. #if 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) */
  232. NSThread *_runOnThread;
  233. #endif
  234. }
  235. #pragma mark - PUBLIC: Shared HID Remote
  236. + (HIDRemote *)sharedHIDRemote;
  237. #pragma mark - PUBLIC: System Information
  238. + (BOOL)isCandelairInstalled;
  239. + (BOOL)isCandelairInstallationRequiredForRemoteMode:(HIDRemoteMode)remoteMode;
  240. + (SInt32)OSXVersion;
  241. - (HIDRemoteAluminumRemoteSupportLevel)aluminiumRemoteSystemSupportLevel;
  242. #pragma mark - PUBLIC: Interface / API
  243. - (BOOL)startRemoteControl:(HIDRemoteMode)hidRemoteMode;
  244. - (void)stopRemoteControl;
  245. - (BOOL)isStarted;
  246. - (HIDRemoteMode)startedInMode;
  247. - (unsigned)activeRemoteControlCount;
  248. - (SInt32)lastSeenRemoteControlID;
  249. - (void)setLastSeenModel:(HIDRemoteModel)aModel;
  250. - (HIDRemoteModel)lastSeenModel;
  251. - (void)setDelegate:(NSObject <HIDRemoteDelegate> *)newDelegate;
  252. - (NSObject <HIDRemoteDelegate> *)delegate;
  253. - (void)setSimulateHoldEvents:(BOOL)newSimulateHoldEvents;
  254. - (BOOL)simulateHoldEvents;
  255. - (void)setUnusedButtonCodes:(NSArray *)newArrayWithUnusedButtonCodesAsNSNumbers;
  256. - (NSArray *)unusedButtonCodes;
  257. #pragma mark - PUBLIC: Expert APIs
  258. - (void)setEnableSecureEventInputWorkaround:(BOOL)newEnableSecureEventInputWorkaround;
  259. - (BOOL)enableSecureEventInputWorkaround;
  260. - (void)setExclusiveLockLendingEnabled:(BOOL)newExclusiveLockLendingEnabled;
  261. - (BOOL)exclusiveLockLendingEnabled;
  262. - (BOOL)isApplicationTerminating;
  263. - (BOOL)isStopping;
  264. #pragma mark - PRIVATE: HID Event handling
  265. - (void)_handleButtonCode:(HIDRemoteButtonCode)buttonCode isPressed:(BOOL)isPressed hidAttribsDict:(NSMutableDictionary *)hidAttribsDict;
  266. - (void)_sendButtonCode:(HIDRemoteButtonCode)buttonCode isPressed:(BOOL)isPressed hidAttribsDict:(NSMutableDictionary *)hidAttribsDict;
  267. - (void)_hidEventFor:(io_service_t)hidDevice from:(IOHIDQueueInterface **)interface withResult:(IOReturn)result;
  268. #pragma mark - PRIVATE: Service setup and destruction
  269. - (BOOL)_prematchService:(io_object_t)service;
  270. - (HIDRemoteButtonCode)buttonCodeForUsage:(unsigned int)usage usagePage:(unsigned int)usagePage;
  271. - (BOOL)_setupService:(io_object_t)service;
  272. - (void)_destructService:(io_object_t)service;
  273. #pragma mark - PRIVATE: Distributed notifiations handling
  274. - (void)_postStatusWithAction:(NSString *)action;
  275. - (void)_handleNotifications:(NSNotification *)notification;
  276. - (void)_setSendStatusNotifications:(BOOL)doSend;
  277. - (BOOL)_sendStatusNotifications;
  278. #pragma mark - PRIVATE: Application becomes active / inactive handling for kHIDRemoteModeExclusiveAuto
  279. - (void)_appStatusChanged:(NSNotification *)notification;
  280. - (void)_delayedAutoRecovery:(NSTimer *)aTimer;
  281. #pragma mark - PRIVATE: Notification handling
  282. - (void)_serviceMatching:(io_iterator_t)iterator;
  283. - (void)_serviceNotificationFor:(io_service_t)service messageType:(natural_t)messageType messageArgument:(void *)messageArgument;
  284. - (void)_updateSessionInformation;
  285. - (void)_secureInputNotificationFor:(io_service_t)service messageType:(natural_t)messageType messageArgument:(void *)messageArgument;
  286. @end
  287. #pragma mark - Information attribute keys
  288. extern NSString *kHIDRemoteManufacturer;
  289. extern NSString *kHIDRemoteProduct;
  290. extern NSString *kHIDRemoteTransport;
  291. #pragma mark - Internal/Expert attribute keys (AKA: don't touch these unless you really, really, REALLY know what you do)
  292. extern NSString *kHIDRemoteCFPluginInterface;
  293. extern NSString *kHIDRemoteHIDDeviceInterface;
  294. extern NSString *kHIDRemoteCookieButtonCodeLUT;
  295. extern NSString *kHIDRemoteHIDQueueInterface;
  296. extern NSString *kHIDRemoteServiceNotification;
  297. extern NSString *kHIDRemoteCFRunLoopSource;
  298. extern NSString *kHIDRemoteLastButtonPressed;
  299. extern NSString *kHIDRemoteService;
  300. extern NSString *kHIDRemoteSimulateHoldEventsTimer;
  301. extern NSString *kHIDRemoteSimulateHoldEventsOriginButtonCode;
  302. extern NSString *kHIDRemoteAluminumRemoteSupportLevel;
  303. extern NSString *kHIDRemoteAluminumRemoteSupportOnDemand;
  304. #pragma mark - Distributed notifications
  305. extern NSString *kHIDRemoteDNHIDRemotePing;
  306. extern NSString *kHIDRemoteDNHIDRemoteRetry;
  307. extern NSString *kHIDRemoteDNHIDRemoteStatus;
  308. extern NSString *kHIDRemoteDNHIDRemoteRetryGlobalObject;
  309. #pragma mark - Distributed notifications userInfo keys and values
  310. extern NSString *kHIDRemoteDNStatusHIDRemoteVersionKey;
  311. extern NSString *kHIDRemoteDNStatusPIDKey;
  312. extern NSString *kHIDRemoteDNStatusModeKey;
  313. extern NSString *kHIDRemoteDNStatusUnusedButtonCodesKey;
  314. extern NSString *kHIDRemoteDNStatusRemoteControlCountKey;
  315. extern NSString *kHIDRemoteDNStatusReturnToPIDKey;
  316. extern NSString *kHIDRemoteDNStatusActionKey;
  317. extern NSString *kHIDRemoteDNStatusActionStart;
  318. extern NSString *kHIDRemoteDNStatusActionStop;
  319. extern NSString *kHIDRemoteDNStatusActionUpdate;
  320. extern NSString *kHIDRemoteDNStatusActionNoNeed;
  321. #pragma mark - Driver compatibility flags
  322. #ifndef HID_REMOTE_COMPATIBILITY_FLAGS_ENUM
  323. #define HID_REMOTE_COMPATIBILITY_FLAGS_ENUM 1
  324. typedef enum
  325. {
  326. kHIDRemoteCompatibilityFlagsStandardHIDRemoteDevice = 1L,
  327. } HIDRemoteCompatibilityFlags;
  328. #endif /* HID_REMOTE_COMPATIBILITY_FLAGS_ENUM */