HIDRemote.m 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188
  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 "HIDRemote.h"
  51. // ARC support
  52. #if !__has_feature(objc_arc)
  53. #define HIDRemoteRetain(object) [object retain]
  54. #define HIDRemoteRetained(object) [object retain]
  55. #define HIDRemoteRelease(object) [object release]
  56. #define HIDRemoteReleaseNil(object) [object release]; object=nil
  57. #define HIDRemoteAutoreleased(object) [object autorelease]
  58. #define HIDRemoteSuperDealloc(object) [super dealloc]
  59. #define HIDRemoteAutoreleasePoolOpen() NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  60. #define HIDRemoteAutoreleasePoolClose() [pool release];
  61. #define __HIDRemoteBridge
  62. #define HIDRemoteBridgingRelease
  63. #define HIDRemoteBridgingRetain
  64. #else /* !__has_feature(objc_arc) */
  65. #define HIDRemoteRetain(object)
  66. #define HIDRemoteRetained(object) object
  67. #define HIDRemoteRelease(object)
  68. #define HIDRemoteReleaseNil(object) object=nil
  69. #define HIDRemoteAutoreleased(object) object
  70. #define HIDRemoteSuperDealloc(object)
  71. #define HIDRemoteAutoreleasePoolOpen() @autoreleasepool {
  72. #define HIDRemoteAutoreleasePoolClose() }
  73. #define __HIDRemoteBridge __bridge
  74. #define HIDRemoteBridgingRelease CFBridgingRelease
  75. #define HIDRemoteBridgingRetain CFBridgingRetain
  76. #endif
  77. // Callback Prototypes
  78. static void HIDEventCallback( void * target,
  79. IOReturn result,
  80. void * refcon,
  81. void * sender);
  82. static void ServiceMatchingCallback( void *refCon,
  83. io_iterator_t iterator);
  84. static void ServiceNotificationCallback(void * refCon,
  85. io_service_t service,
  86. natural_t messageType,
  87. void * messageArgument);
  88. static void SecureInputNotificationCallback( void * refCon,
  89. io_service_t service,
  90. natural_t messageType,
  91. void * messageArgument);
  92. // Shared HIDRemote instance
  93. static HIDRemote *sHIDRemote = nil;
  94. @implementation HIDRemote
  95. #pragma mark - Init, dealloc & shared instance
  96. + (HIDRemote *)sharedHIDRemote
  97. {
  98. if (sHIDRemote==nil)
  99. {
  100. sHIDRemote = [[HIDRemote alloc] init];
  101. }
  102. return (sHIDRemote);
  103. }
  104. - (id)init
  105. {
  106. if ((self = [super init]) != nil)
  107. {
  108. #if HIDREMOTE_THREADSAFETY_HARDENED_NOTIFICATION_HANDLING
  109. _runOnThread = HIDRemoteRetained([NSThread currentThread]);
  110. #endif
  111. // Detect application becoming active/inactive
  112. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_appStatusChanged:) name:NSApplicationDidBecomeActiveNotification object:[NSApplication sharedApplication]];
  113. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_appStatusChanged:) name:NSApplicationWillResignActiveNotification object:[NSApplication sharedApplication]];
  114. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_appStatusChanged:) name:NSApplicationWillTerminateNotification object:[NSApplication sharedApplication]];
  115. // Handle distributed notifications
  116. _pidString = [[NSString alloc] initWithFormat:@"%d", getpid()];
  117. [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleNotifications:) name:kHIDRemoteDNHIDRemotePing object:nil];
  118. [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleNotifications:) name:kHIDRemoteDNHIDRemoteRetry object:kHIDRemoteDNHIDRemoteRetryGlobalObject];
  119. [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleNotifications:) name:kHIDRemoteDNHIDRemoteRetry object:_pidString];
  120. // Enabled by default: simulate hold events for plus/minus
  121. _simulateHoldEvents = YES;
  122. // Enabled by default: work around for a locking issue introduced with Security Update 2008-004 / 10.4.9 and beyond (credit for finding this workaround goes to Martin Kahr)
  123. _secureEventInputWorkAround = YES;
  124. _secureInputNotification = 0;
  125. // Initialize instance variables
  126. _lastSeenRemoteID = -1;
  127. _lastSeenModel = kHIDRemoteModelUndetermined;
  128. _unusedButtonCodes = [[NSMutableArray alloc] init];
  129. _exclusiveLockLending = NO;
  130. _sendExclusiveResourceReuseNotification = YES;
  131. _applicationIsTerminating = NO;
  132. // Send status notifications
  133. _sendStatusNotifications = YES;
  134. }
  135. return (self);
  136. }
  137. - (void)dealloc
  138. {
  139. [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:[NSApplication sharedApplication]];
  140. [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillResignActiveNotification object:[NSApplication sharedApplication]];
  141. [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationDidBecomeActiveNotification object:[NSApplication sharedApplication]];
  142. [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kHIDRemoteDNHIDRemotePing object:nil];
  143. [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kHIDRemoteDNHIDRemoteRetry object:kHIDRemoteDNHIDRemoteRetryGlobalObject];
  144. [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kHIDRemoteDNHIDRemoteRetry object:_pidString];
  145. [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:nil object:nil]; /* As demanded by the documentation for -[NSDistributedNotificationCenter removeObserver:name:object:] */
  146. [self stopRemoteControl];
  147. [self setExclusiveLockLendingEnabled:NO];
  148. [self setDelegate:nil];
  149. if (_unusedButtonCodes != nil)
  150. {
  151. HIDRemoteReleaseNil(_unusedButtonCodes);
  152. }
  153. #if HIDREMOTE_THREADSAFETY_HARDENED_NOTIFICATION_HANDLING
  154. HIDRemoteReleaseNil(_runOnThread);
  155. #endif
  156. HIDRemoteReleaseNil(_pidString);
  157. HIDRemoteSuperDealloc();
  158. }
  159. #pragma mark - PUBLIC: System Information
  160. + (BOOL)isCandelairInstalled
  161. {
  162. mach_port_t masterPort = 0;
  163. kern_return_t kernResult;
  164. io_service_t matchingService = 0;
  165. BOOL isInstalled = NO;
  166. kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
  167. if ((kernResult!=kIOReturnSuccess) || (masterPort==0)) { return(NO); }
  168. if ((matchingService = IOServiceGetMatchingService(masterPort, IOServiceMatching("IOSPIRITIRController"))) != 0)
  169. {
  170. isInstalled = YES;
  171. IOObjectRelease((io_object_t) matchingService);
  172. }
  173. mach_port_deallocate(mach_task_self(), masterPort);
  174. return (isInstalled);
  175. }
  176. + (BOOL)isCandelairInstallationRequiredForRemoteMode:(HIDRemoteMode)remoteMode
  177. {
  178. // Determine OS version
  179. switch ([self OSXVersion])
  180. {
  181. case 0x1060: // OS 10.6
  182. case 0x1061: // OS 10.6.1
  183. // OS X 10.6(.0) and OS X 10.6.1 require the Candelair driver for to be installed,
  184. // so that third party apps can acquire an exclusive lock on the receiver HID Device
  185. // via IOKit.
  186. switch (remoteMode)
  187. {
  188. case kHIDRemoteModeExclusive:
  189. case kHIDRemoteModeExclusiveAuto:
  190. if (![self isCandelairInstalled])
  191. {
  192. return (YES);
  193. }
  194. break;
  195. default:
  196. return (NO);
  197. break;
  198. }
  199. break;
  200. }
  201. return (NO);
  202. }
  203. // Drop-in replacement for Gestalt(gestaltSystemVersion, &osXVersion) that avoids use of Gestalt for code targeting 10.10 or later
  204. + (SInt32)OSXVersion
  205. {
  206. static SInt32 sHRGestaltOSXVersion = 0;
  207. if (sHRGestaltOSXVersion==0)
  208. {
  209. #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
  210. // Code for builds targeting OS X 10.10+
  211. NSOperatingSystemVersion osVersion;
  212. osVersion = [[NSProcessInfo processInfo] operatingSystemVersion];
  213. sHRGestaltOSXVersion = (SInt32)(0x01000 | ((osVersion.majorVersion-10)<<8) | (osVersion.minorVersion<<4) | osVersion.patchVersion);
  214. #else
  215. #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_9
  216. // Code for builds using the OS X 10.10 SDK or later
  217. NSOperatingSystemVersion osVersion;
  218. if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)])
  219. {
  220. osVersion = [[NSProcessInfo processInfo] operatingSystemVersion];
  221. sHRGestaltOSXVersion = (SInt32)(0x01000 | ((osVersion.majorVersion-10)<<8) | (osVersion.minorVersion<<4) | osVersion.patchVersion);
  222. }
  223. else
  224. {
  225. #pragma clang diagnostic push
  226. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  227. Gestalt (gestaltSystemVersion, &sHRGestaltOSXVersion);
  228. #pragma clang diagnostic pop
  229. }
  230. #else /* MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_9 */
  231. // Code for builds using an SDK older than 10.10
  232. #pragma clang diagnostic push
  233. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  234. Gestalt (gestaltSystemVersion, &sHRGestaltOSXVersion);
  235. #pragma clang diagnostic pop
  236. #endif /* MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_9 */
  237. #endif /* MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9 */
  238. }
  239. return (sHRGestaltOSXVersion);
  240. }
  241. - (HIDRemoteAluminumRemoteSupportLevel)aluminiumRemoteSystemSupportLevel
  242. {
  243. HIDRemoteAluminumRemoteSupportLevel supportLevel = kHIDRemoteAluminumRemoteSupportLevelNone;
  244. NSEnumerator *attribDictsEnum;
  245. NSDictionary *hidAttribsDict;
  246. attribDictsEnum = [_serviceAttribMap objectEnumerator];
  247. while ((hidAttribsDict = [attribDictsEnum nextObject]) != nil)
  248. {
  249. NSNumber *deviceSupportLevel;
  250. if ((deviceSupportLevel = [hidAttribsDict objectForKey:kHIDRemoteAluminumRemoteSupportLevel]) != nil)
  251. {
  252. if ([deviceSupportLevel intValue] > (int)supportLevel)
  253. {
  254. supportLevel = [deviceSupportLevel intValue];
  255. }
  256. }
  257. }
  258. return (supportLevel);
  259. }
  260. #pragma mark - PUBLIC: Interface / API
  261. - (BOOL)startRemoteControl:(HIDRemoteMode)hidRemoteMode
  262. {
  263. if ((_mode == kHIDRemoteModeNone) && (hidRemoteMode != kHIDRemoteModeNone))
  264. {
  265. kern_return_t kernReturn;
  266. CFMutableDictionaryRef matchDict=NULL;
  267. io_service_t rootService;
  268. do
  269. {
  270. // Get IOKit master port
  271. kernReturn = IOMasterPort(bootstrap_port, &_masterPort);
  272. if ((kernReturn!=kIOReturnSuccess) || (_masterPort==0)) { break; }
  273. // Setup notification port
  274. _notifyPort = IONotificationPortCreate(_masterPort);
  275. if ((_notifyRLSource = IONotificationPortGetRunLoopSource(_notifyPort)) != NULL)
  276. {
  277. CFRunLoopAddSource( CFRunLoopGetCurrent(),
  278. _notifyRLSource,
  279. kCFRunLoopCommonModes);
  280. }
  281. else
  282. {
  283. break;
  284. }
  285. // Setup SecureInput notification
  286. if ((hidRemoteMode == kHIDRemoteModeExclusive) || (hidRemoteMode == kHIDRemoteModeExclusiveAuto))
  287. {
  288. if ((rootService = IORegistryEntryFromPath(_masterPort, kIOServicePlane ":/")) != 0)
  289. {
  290. kernReturn = IOServiceAddInterestNotification( _notifyPort,
  291. rootService,
  292. kIOBusyInterest,
  293. SecureInputNotificationCallback,
  294. (__HIDRemoteBridge void *)self,
  295. &_secureInputNotification);
  296. if (kernReturn != kIOReturnSuccess) { break; }
  297. [self _updateSessionInformation];
  298. }
  299. else
  300. {
  301. break;
  302. }
  303. }
  304. // Setup notification matching dict
  305. matchDict = IOServiceMatching(kIOHIDDeviceKey);
  306. CFRetain(matchDict);
  307. // Actually add notification
  308. kernReturn = IOServiceAddMatchingNotification( _notifyPort,
  309. kIOFirstMatchNotification,
  310. matchDict, // one reference count consumed by this call
  311. ServiceMatchingCallback,
  312. (__HIDRemoteBridge void *) self,
  313. &_matchingServicesIterator);
  314. if (kernReturn != kIOReturnSuccess) { break; }
  315. // Setup serviceAttribMap
  316. _serviceAttribMap = [[NSMutableDictionary alloc] init];
  317. if (_serviceAttribMap==nil) { break; }
  318. // Phew .. everything went well!
  319. _mode = hidRemoteMode;
  320. CFRelease(matchDict);
  321. [self _serviceMatching:_matchingServicesIterator];
  322. [self _postStatusWithAction:kHIDRemoteDNStatusActionStart];
  323. // Register for system wake notifications
  324. [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(_computerDidWake:) name:NSWorkspaceDidWakeNotification object:nil];
  325. return (YES);
  326. }while(0);
  327. // An error occured. Do necessary clean up.
  328. if (matchDict!=NULL)
  329. {
  330. CFRelease(matchDict);
  331. matchDict = NULL;
  332. }
  333. [self stopRemoteControl];
  334. }
  335. return (NO);
  336. }
  337. - (void)stopRemoteControl
  338. {
  339. UInt32 serviceCount = 0;
  340. _autoRecover = NO;
  341. _isStopping = YES;
  342. if (_autoRecoveryTimer!=nil)
  343. {
  344. [_autoRecoveryTimer invalidate];
  345. HIDRemoteReleaseNil(_autoRecoveryTimer);
  346. }
  347. if (_serviceAttribMap!=nil)
  348. {
  349. NSDictionary *cloneDict = [[NSDictionary alloc] initWithDictionary:_serviceAttribMap];
  350. if (cloneDict!=nil)
  351. {
  352. NSEnumerator *mapKeyEnum = [cloneDict keyEnumerator];
  353. NSNumber *serviceValue;
  354. while ((serviceValue = [mapKeyEnum nextObject]) != nil)
  355. {
  356. [self _destructService:(io_object_t)[serviceValue unsignedIntValue]];
  357. serviceCount++;
  358. };
  359. HIDRemoteReleaseNil(cloneDict);
  360. }
  361. HIDRemoteReleaseNil(_serviceAttribMap);
  362. }
  363. if (_matchingServicesIterator!=0)
  364. {
  365. IOObjectRelease((io_object_t) _matchingServicesIterator);
  366. _matchingServicesIterator = 0;
  367. }
  368. if (_secureInputNotification!=0)
  369. {
  370. IOObjectRelease((io_object_t) _secureInputNotification);
  371. _secureInputNotification = 0;
  372. }
  373. if (_notifyRLSource!=NULL)
  374. {
  375. CFRunLoopSourceInvalidate(_notifyRLSource);
  376. _notifyRLSource = NULL;
  377. }
  378. if (_notifyPort!=NULL)
  379. {
  380. IONotificationPortDestroy(_notifyPort);
  381. _notifyPort = NULL;
  382. }
  383. if (_masterPort!=0)
  384. {
  385. mach_port_deallocate(mach_task_self(), _masterPort);
  386. _masterPort = 0;
  387. }
  388. if (_returnToPID!=nil)
  389. {
  390. HIDRemoteReleaseNil(_returnToPID);
  391. }
  392. if (_mode!=kHIDRemoteModeNone)
  393. {
  394. // Post status
  395. [self _postStatusWithAction:kHIDRemoteDNStatusActionStop];
  396. if (_sendStatusNotifications)
  397. {
  398. // In case we were not ready to lend it earlier, tell other HIDRemote apps that the resources (if any were used) are now again available for use by other applications
  399. if (((_mode==kHIDRemoteModeExclusive) || (_mode==kHIDRemoteModeExclusiveAuto)) && (_sendExclusiveResourceReuseNotification==YES) && (_exclusiveLockLending==NO) && (serviceCount>0))
  400. {
  401. _mode = kHIDRemoteModeNone;
  402. if (!_isRestarting)
  403. {
  404. [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kHIDRemoteDNHIDRemoteRetry
  405. object:kHIDRemoteDNHIDRemoteRetryGlobalObject
  406. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
  407. [NSNumber numberWithUnsignedInt:(unsigned int)getpid()], kHIDRemoteDNStatusPIDKey,
  408. [[NSBundle mainBundle] bundleIdentifier], (NSString *)kCFBundleIdentifierKey,
  409. nil]
  410. deliverImmediately:YES];
  411. }
  412. }
  413. }
  414. // Unregister from system wake notifications
  415. [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self name:NSWorkspaceDidWakeNotification object:nil];
  416. }
  417. _mode = kHIDRemoteModeNone;
  418. _isStopping = NO;
  419. }
  420. - (BOOL)isStarted
  421. {
  422. return (_mode != kHIDRemoteModeNone);
  423. }
  424. - (HIDRemoteMode)startedInMode
  425. {
  426. return (_mode);
  427. }
  428. - (unsigned)activeRemoteControlCount
  429. {
  430. return ((unsigned)[_serviceAttribMap count]);
  431. }
  432. - (SInt32)lastSeenRemoteControlID
  433. {
  434. return (_lastSeenRemoteID);
  435. }
  436. - (HIDRemoteModel)lastSeenModel
  437. {
  438. return (_lastSeenModel);
  439. }
  440. - (void)setLastSeenModel:(HIDRemoteModel)aModel
  441. {
  442. _lastSeenModel = aModel;
  443. }
  444. - (void)setSimulateHoldEvents:(BOOL)newSimulateHoldEvents
  445. {
  446. _simulateHoldEvents = newSimulateHoldEvents;
  447. }
  448. - (BOOL)simulateHoldEvents
  449. {
  450. return (_simulateHoldEvents);
  451. }
  452. - (NSArray *)unusedButtonCodes
  453. {
  454. return (_unusedButtonCodes);
  455. }
  456. - (void)setUnusedButtonCodes:(NSArray *)newArrayWithUnusedButtonCodesAsNSNumbers
  457. {
  458. HIDRemoteRetain(newArrayWithUnusedButtonCodesAsNSNumbers);
  459. HIDRemoteRelease(_unusedButtonCodes);
  460. _unusedButtonCodes = newArrayWithUnusedButtonCodesAsNSNumbers;
  461. [self _postStatusWithAction:kHIDRemoteDNStatusActionUpdate];
  462. }
  463. - (void)setDelegate:(NSObject <HIDRemoteDelegate> *)newDelegate
  464. {
  465. _delegate = newDelegate;
  466. }
  467. - (NSObject <HIDRemoteDelegate> *)delegate
  468. {
  469. return (_delegate);
  470. }
  471. #pragma mark - PUBLIC: Expert APIs
  472. - (void)setEnableSecureEventInputWorkaround:(BOOL)newEnableSecureEventInputWorkaround
  473. {
  474. _secureEventInputWorkAround = newEnableSecureEventInputWorkaround;
  475. }
  476. - (BOOL)enableSecureEventInputWorkaround
  477. {
  478. return (_secureEventInputWorkAround);
  479. }
  480. - (void)setExclusiveLockLendingEnabled:(BOOL)newExclusiveLockLendingEnabled
  481. {
  482. if (newExclusiveLockLendingEnabled != _exclusiveLockLending)
  483. {
  484. _exclusiveLockLending = newExclusiveLockLendingEnabled;
  485. if (_exclusiveLockLending)
  486. {
  487. [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleNotifications:) name:kHIDRemoteDNHIDRemoteStatus object:nil];
  488. }
  489. else
  490. {
  491. [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kHIDRemoteDNHIDRemoteStatus object:nil];
  492. HIDRemoteReleaseNil(_waitForReturnByPID);
  493. }
  494. }
  495. }
  496. - (BOOL)exclusiveLockLendingEnabled
  497. {
  498. return (_exclusiveLockLending);
  499. }
  500. - (void)setSendExclusiveResourceReuseNotification:(BOOL)newSendExclusiveResourceReuseNotification
  501. {
  502. _sendExclusiveResourceReuseNotification = newSendExclusiveResourceReuseNotification;
  503. }
  504. - (BOOL)sendExclusiveResourceReuseNotification
  505. {
  506. return (_sendExclusiveResourceReuseNotification);
  507. }
  508. - (BOOL)isApplicationTerminating
  509. {
  510. return (_applicationIsTerminating);
  511. }
  512. - (BOOL)isStopping
  513. {
  514. return (_isStopping);
  515. }
  516. #pragma mark - PRIVATE: Application becomes active / inactive handling for kHIDRemoteModeExclusiveAuto
  517. - (void)_appStatusChanged:(NSNotification *)notification
  518. {
  519. #if HIDREMOTE_THREADSAFETY_HARDENED_NOTIFICATION_HANDLING
  520. if ([self respondsToSelector:@selector(performSelector:onThread:withObject:waitUntilDone:)]) // OS X 10.5+ only
  521. {
  522. if ([NSThread currentThread] != _runOnThread)
  523. {
  524. if ([[notification name] isEqual:NSApplicationDidBecomeActiveNotification])
  525. {
  526. if (!_autoRecover)
  527. {
  528. return;
  529. }
  530. }
  531. if ([[notification name] isEqual:NSApplicationWillResignActiveNotification])
  532. {
  533. if (_mode != kHIDRemoteModeExclusiveAuto)
  534. {
  535. return;
  536. }
  537. }
  538. [self performSelector:@selector(_appStatusChanged:) onThread:_runOnThread withObject:notification waitUntilDone:[[notification name] isEqual:NSApplicationWillTerminateNotification]];
  539. return;
  540. }
  541. }
  542. #endif
  543. if (notification!=nil)
  544. {
  545. if (_autoRecoveryTimer!=nil)
  546. {
  547. [_autoRecoveryTimer invalidate];
  548. HIDRemoteReleaseNil(_autoRecoveryTimer);
  549. }
  550. if ([[notification name] isEqual:NSApplicationDidBecomeActiveNotification])
  551. {
  552. if (_autoRecover)
  553. {
  554. // Delay autorecover by 0.1 to avoid race conditions
  555. if ((_autoRecoveryTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:0.1] interval:0.1 target:self selector:@selector(_delayedAutoRecovery:) userInfo:nil repeats:NO]) != nil)
  556. {
  557. // Using CFRunLoopAddTimer instead of [[NSRunLoop currentRunLoop] addTimer:.. for consistency with run loop modes.
  558. // The kCFRunLoopCommonModes counterpart NSRunLoopCommonModes is only available in 10.5 and later, whereas this code
  559. // is designed to be also compatible with 10.4. CFRunLoopTimerRef is "toll-free-bridged" with NSTimer since 10.0.
  560. CFRunLoopAddTimer(CFRunLoopGetCurrent(), (CFRunLoopTimerRef)_autoRecoveryTimer, kCFRunLoopCommonModes);
  561. }
  562. }
  563. }
  564. if ([[notification name] isEqual:NSApplicationWillResignActiveNotification])
  565. {
  566. if (_mode == kHIDRemoteModeExclusiveAuto)
  567. {
  568. [self stopRemoteControl];
  569. _autoRecover = YES;
  570. }
  571. }
  572. if ([[notification name] isEqual:NSApplicationWillTerminateNotification])
  573. {
  574. _applicationIsTerminating = YES;
  575. if ([self isStarted])
  576. {
  577. [self stopRemoteControl];
  578. }
  579. }
  580. }
  581. }
  582. - (void)_delayedAutoRecovery:(NSTimer *)aTimer
  583. {
  584. [_autoRecoveryTimer invalidate];
  585. HIDRemoteReleaseNil(_autoRecoveryTimer);
  586. if (_autoRecover)
  587. {
  588. [self startRemoteControl:kHIDRemoteModeExclusiveAuto];
  589. _autoRecover = NO;
  590. }
  591. }
  592. #pragma mark - PRIVATE: Distributed notifiations handling
  593. - (void)_postStatusWithAction:(NSString *)action
  594. {
  595. if (_sendStatusNotifications)
  596. {
  597. [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kHIDRemoteDNHIDRemoteStatus
  598. object:((_pidString!=nil) ? _pidString : [NSString stringWithFormat:@"%d",getpid()])
  599. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
  600. [NSNumber numberWithInt:1], kHIDRemoteDNStatusHIDRemoteVersionKey,
  601. [NSNumber numberWithUnsignedInt:(unsigned int)getpid()], kHIDRemoteDNStatusPIDKey,
  602. [NSNumber numberWithInt:(int)_mode], kHIDRemoteDNStatusModeKey,
  603. [NSNumber numberWithUnsignedInt:(unsigned int)[self activeRemoteControlCount]], kHIDRemoteDNStatusRemoteControlCountKey,
  604. ((_unusedButtonCodes!=nil) ? _unusedButtonCodes : [NSArray array]), kHIDRemoteDNStatusUnusedButtonCodesKey,
  605. action, kHIDRemoteDNStatusActionKey,
  606. [[NSBundle mainBundle] bundleIdentifier], (NSString *)kCFBundleIdentifierKey,
  607. _returnToPID, kHIDRemoteDNStatusReturnToPIDKey,
  608. nil]
  609. deliverImmediately:YES
  610. ];
  611. }
  612. }
  613. - (void)_handleNotifications:(NSNotification *)notification
  614. {
  615. NSString *notificationName;
  616. #if HIDREMOTE_THREADSAFETY_HARDENED_NOTIFICATION_HANDLING
  617. if ([self respondsToSelector:@selector(performSelector:onThread:withObject:waitUntilDone:)]) // OS X 10.5+ only
  618. {
  619. if ([NSThread currentThread] != _runOnThread)
  620. {
  621. [self performSelector:@selector(_handleNotifications:) onThread:_runOnThread withObject:notification waitUntilDone:NO];
  622. return;
  623. }
  624. }
  625. #endif
  626. if ((notification!=nil) && ((notificationName = [notification name]) != nil))
  627. {
  628. if ([notificationName isEqual:kHIDRemoteDNHIDRemotePing])
  629. {
  630. [self _postStatusWithAction:kHIDRemoteDNStatusActionUpdate];
  631. }
  632. if ([notificationName isEqual:kHIDRemoteDNHIDRemoteRetry])
  633. {
  634. if ([self isStarted])
  635. {
  636. BOOL retry = YES;
  637. // Ignore our own global retry broadcasts
  638. if ([[notification object] isEqual:kHIDRemoteDNHIDRemoteRetryGlobalObject])
  639. {
  640. NSNumber *fromPID;
  641. if ((fromPID = [[notification userInfo] objectForKey:kHIDRemoteDNStatusPIDKey]) != nil)
  642. {
  643. if (getpid() == (int)[fromPID unsignedIntValue])
  644. {
  645. retry = NO;
  646. }
  647. }
  648. }
  649. if (retry)
  650. {
  651. if (([self delegate] != nil) &&
  652. ([[self delegate] respondsToSelector:@selector(hidRemote:shouldRetryExclusiveLockWithInfo:)]))
  653. {
  654. retry = [[self delegate] hidRemote:self shouldRetryExclusiveLockWithInfo:[notification userInfo]];
  655. }
  656. }
  657. if (retry)
  658. {
  659. HIDRemoteMode restartInMode = _mode;
  660. if (restartInMode != kHIDRemoteModeNone)
  661. {
  662. _isRestarting = YES;
  663. [self stopRemoteControl];
  664. HIDRemoteReleaseNil(_returnToPID);
  665. [self startRemoteControl:restartInMode];
  666. _isRestarting = NO;
  667. if (restartInMode != kHIDRemoteModeShared)
  668. {
  669. _returnToPID = HIDRemoteRetained([[notification userInfo] objectForKey:kHIDRemoteDNStatusPIDKey]);
  670. }
  671. }
  672. }
  673. else
  674. {
  675. NSNumber *cacheReturnPID = _returnToPID;
  676. _returnToPID = HIDRemoteRetained([[notification userInfo] objectForKey:kHIDRemoteDNStatusPIDKey]);
  677. [self _postStatusWithAction:kHIDRemoteDNStatusActionNoNeed];
  678. HIDRemoteRelease(_returnToPID);
  679. _returnToPID = cacheReturnPID;
  680. }
  681. }
  682. }
  683. if (_exclusiveLockLending)
  684. {
  685. if ([notificationName isEqual:kHIDRemoteDNHIDRemoteStatus])
  686. {
  687. NSString *action;
  688. if ((action = [[notification userInfo] objectForKey:kHIDRemoteDNStatusActionKey]) != nil)
  689. {
  690. if ((_mode == kHIDRemoteModeNone) && (_waitForReturnByPID!=nil))
  691. {
  692. NSNumber *pidNumber, *returnToPIDNumber;
  693. if ((pidNumber = [[notification userInfo] objectForKey:kHIDRemoteDNStatusPIDKey]) != nil)
  694. {
  695. returnToPIDNumber = [[notification userInfo] objectForKey:kHIDRemoteDNStatusReturnToPIDKey];
  696. if ([action isEqual:kHIDRemoteDNStatusActionStart])
  697. {
  698. if ([pidNumber isEqual:_waitForReturnByPID])
  699. {
  700. NSNumber *startMode;
  701. if ((startMode = [[notification userInfo] objectForKey:kHIDRemoteDNStatusModeKey]) != nil)
  702. {
  703. if ([startMode intValue] == kHIDRemoteModeShared)
  704. {
  705. returnToPIDNumber = [NSNumber numberWithInt:getpid()];
  706. action = kHIDRemoteDNStatusActionNoNeed;
  707. }
  708. }
  709. }
  710. }
  711. if (returnToPIDNumber != nil)
  712. {
  713. if ([action isEqual:kHIDRemoteDNStatusActionStop] || [action isEqual:kHIDRemoteDNStatusActionNoNeed])
  714. {
  715. if ([pidNumber isEqual:_waitForReturnByPID] && ([returnToPIDNumber intValue] == getpid()))
  716. {
  717. HIDRemoteReleaseNil(_waitForReturnByPID);
  718. if (([self delegate] != nil) &&
  719. ([[self delegate] respondsToSelector:@selector(hidRemote:exclusiveLockReleasedByApplicationWithInfo:)]))
  720. {
  721. [[self delegate] hidRemote:self exclusiveLockReleasedByApplicationWithInfo:[notification userInfo]];
  722. }
  723. else
  724. {
  725. [self startRemoteControl:kHIDRemoteModeExclusive];
  726. }
  727. }
  728. }
  729. }
  730. }
  731. }
  732. if (_mode==kHIDRemoteModeExclusive)
  733. {
  734. if ([action isEqual:kHIDRemoteDNStatusActionStart])
  735. {
  736. NSNumber *originPID = [[notification userInfo] objectForKey:kHIDRemoteDNStatusPIDKey];
  737. BOOL lendLock = YES;
  738. if ([originPID intValue] != getpid())
  739. {
  740. if (([self delegate] != nil) &&
  741. ([[self delegate] respondsToSelector:@selector(hidRemote:lendExclusiveLockToApplicationWithInfo:)]))
  742. {
  743. lendLock = [[self delegate] hidRemote:self lendExclusiveLockToApplicationWithInfo:[notification userInfo]];
  744. }
  745. if (lendLock)
  746. {
  747. HIDRemoteRelease(_waitForReturnByPID);
  748. _waitForReturnByPID = HIDRemoteRetained(originPID);
  749. if (_waitForReturnByPID != nil)
  750. {
  751. [self stopRemoteControl];
  752. [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kHIDRemoteDNHIDRemoteRetry
  753. object:[NSString stringWithFormat:@"%d", [_waitForReturnByPID intValue]]
  754. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
  755. [NSNumber numberWithUnsignedInt:(unsigned int)getpid()], kHIDRemoteDNStatusPIDKey,
  756. [[NSBundle mainBundle] bundleIdentifier], (NSString *)kCFBundleIdentifierKey,
  757. nil]
  758. deliverImmediately:YES];
  759. }
  760. }
  761. }
  762. }
  763. }
  764. }
  765. }
  766. }
  767. }
  768. }
  769. - (void)_setSendStatusNotifications:(BOOL)doSend
  770. {
  771. _sendStatusNotifications = doSend;
  772. }
  773. - (BOOL)_sendStatusNotifications
  774. {
  775. return (_sendStatusNotifications);
  776. }
  777. #pragma mark - PRIVATE: Service setup and destruction
  778. - (BOOL)_prematchService:(io_object_t)service
  779. {
  780. BOOL serviceMatches = NO;
  781. NSString *ioClass;
  782. NSNumber *candelairHIDRemoteCompatibilityMask;
  783. if (service != 0)
  784. {
  785. // IOClass matching
  786. if ((ioClass = (__HIDRemoteBridge NSString *)IORegistryEntryCreateCFProperty((io_registry_entry_t)service,
  787. CFSTR(kIOClassKey),
  788. kCFAllocatorDefault,
  789. 0)) != nil)
  790. {
  791. // Match on Apple's AppleIRController and old versions of the Remote Buddy IR Controller
  792. if ([ioClass isEqual:@"AppleIRController"] || [ioClass isEqual:@"RBIOKitAIREmu"])
  793. {
  794. CFTypeRef candelairHIDRemoteCompatibilityDevice;
  795. serviceMatches = YES;
  796. if ((candelairHIDRemoteCompatibilityDevice = IORegistryEntryCreateCFProperty((io_registry_entry_t)service, CFSTR("CandelairHIDRemoteCompatibilityDevice"), kCFAllocatorDefault, 0)) != NULL)
  797. {
  798. if (CFEqual(kCFBooleanTrue, candelairHIDRemoteCompatibilityDevice))
  799. {
  800. serviceMatches = NO;
  801. }
  802. CFRelease (candelairHIDRemoteCompatibilityDevice);
  803. }
  804. }
  805. // Match on the virtual IOSPIRIT IR Controller
  806. if ([ioClass isEqual:@"IOSPIRITIRController"])
  807. {
  808. serviceMatches = YES;
  809. }
  810. CFRelease((CFTypeRef)ioClass);
  811. }
  812. // Match on services that claim compatibility with the HID Remote class (Candelair or third-party) by having a property of CandelairHIDRemoteCompatibilityMask = 1 <Type: Number>
  813. if ((candelairHIDRemoteCompatibilityMask = (__HIDRemoteBridge NSNumber *)IORegistryEntryCreateCFProperty((io_registry_entry_t)service, CFSTR("CandelairHIDRemoteCompatibilityMask"), kCFAllocatorDefault, 0)) != nil)
  814. {
  815. if ([candelairHIDRemoteCompatibilityMask isKindOfClass:[NSNumber class]])
  816. {
  817. if ([candelairHIDRemoteCompatibilityMask unsignedIntValue] & kHIDRemoteCompatibilityFlagsStandardHIDRemoteDevice)
  818. {
  819. serviceMatches = YES;
  820. }
  821. else
  822. {
  823. serviceMatches = NO;
  824. }
  825. }
  826. CFRelease((CFTypeRef)candelairHIDRemoteCompatibilityMask);
  827. }
  828. }
  829. if (([self delegate]!=nil) &&
  830. ([[self delegate] respondsToSelector:@selector(hidRemote:inspectNewHardwareWithService:prematchResult:)]))
  831. {
  832. serviceMatches = [((NSObject <HIDRemoteDelegate> *)[self delegate]) hidRemote:self inspectNewHardwareWithService:service prematchResult:serviceMatches];
  833. }
  834. return (serviceMatches);
  835. }
  836. - (HIDRemoteButtonCode)buttonCodeForUsage:(unsigned int)usage usagePage:(unsigned int)usagePage
  837. {
  838. HIDRemoteButtonCode buttonCode = kHIDRemoteButtonCodeNone;
  839. switch (usagePage)
  840. {
  841. case kHIDPage_Consumer:
  842. switch (usage)
  843. {
  844. case kHIDUsage_Csmr_MenuPick:
  845. // Aluminum Remote: Center
  846. buttonCode = (kHIDRemoteButtonCodeCenter|kHIDRemoteButtonCodeAluminumMask);
  847. break;
  848. case kHIDUsage_Csmr_ModeStep:
  849. // Aluminium Remote: Center Hold
  850. buttonCode = (kHIDRemoteButtonCodeCenterHold|kHIDRemoteButtonCodeAluminumMask);
  851. break;
  852. case kHIDUsage_Csmr_PlayOrPause:
  853. // Aluminum Remote: Play/Pause
  854. buttonCode = (kHIDRemoteButtonCodePlay|kHIDRemoteButtonCodeAluminumMask);
  855. break;
  856. case kHIDUsage_Csmr_Rewind:
  857. buttonCode = kHIDRemoteButtonCodeLeftHold;
  858. break;
  859. case kHIDUsage_Csmr_FastForward:
  860. buttonCode = kHIDRemoteButtonCodeRightHold;
  861. break;
  862. case kHIDUsage_Csmr_Menu:
  863. buttonCode = kHIDRemoteButtonCodeMenuHold;
  864. break;
  865. case kHIDUsage_Csmr_VolumeIncrement:
  866. buttonCode = kHIDRemoteButtonCodeUp;
  867. break;
  868. case kHIDUsage_Csmr_VolumeDecrement:
  869. buttonCode = kHIDRemoteButtonCodeDown;
  870. break;
  871. }
  872. break;
  873. case kHIDPage_GenericDesktop:
  874. switch (usage)
  875. {
  876. case kHIDUsage_GD_SystemAppMenu:
  877. buttonCode = kHIDRemoteButtonCodeMenu;
  878. break;
  879. case kHIDUsage_GD_SystemMenu:
  880. buttonCode = kHIDRemoteButtonCodeCenter;
  881. break;
  882. case kHIDUsage_GD_SystemMenuRight:
  883. buttonCode = kHIDRemoteButtonCodeRight;
  884. break;
  885. case kHIDUsage_GD_SystemMenuLeft:
  886. buttonCode = kHIDRemoteButtonCodeLeft;
  887. break;
  888. case kHIDUsage_GD_SystemMenuUp:
  889. buttonCode = kHIDRemoteButtonCodeUp;
  890. break;
  891. case kHIDUsage_GD_SystemMenuDown:
  892. buttonCode = kHIDRemoteButtonCodeDown;
  893. break;
  894. }
  895. break;
  896. case 0x06: /* Reserved */
  897. switch (usage)
  898. {
  899. case 0x22:
  900. buttonCode = kHIDRemoteButtonCodeIDChanged;
  901. break;
  902. }
  903. break;
  904. case 0xFF01: /* Vendor specific */
  905. switch (usage)
  906. {
  907. case 0x23:
  908. buttonCode = kHIDRemoteButtonCodeCenterHold;
  909. break;
  910. #ifdef _HIDREMOTE_EXTENSIONS
  911. #define _HIDREMOTE_EXTENSIONS_SECTION 2
  912. #include "HIDRemoteAdditions.h"
  913. #undef _HIDREMOTE_EXTENSIONS_SECTION
  914. #endif /* _HIDREMOTE_EXTENSIONS */
  915. }
  916. break;
  917. }
  918. return (buttonCode);
  919. }
  920. - (BOOL)_setupService:(io_object_t)service
  921. {
  922. kern_return_t kernResult;
  923. IOReturn returnCode;
  924. HRESULT hResult;
  925. SInt32 score;
  926. BOOL opened = NO, queueStarted = NO;
  927. IOHIDDeviceInterface122 **hidDeviceInterface = NULL;
  928. IOCFPlugInInterface **cfPluginInterface = NULL;
  929. IOHIDQueueInterface **hidQueueInterface = NULL;
  930. io_object_t serviceNotification = 0;
  931. CFRunLoopSourceRef queueEventSource = NULL;
  932. NSMutableDictionary *hidAttribsDict = nil;
  933. CFArrayRef hidElements = NULL;
  934. NSError *error = nil;
  935. UInt32 errorCode = 0;
  936. if (![self _prematchService:service])
  937. {
  938. return (NO);
  939. }
  940. do
  941. {
  942. // Create a plugin interface ..
  943. kernResult = IOCreatePlugInInterfaceForService( service,
  944. kIOHIDDeviceUserClientTypeID,
  945. kIOCFPlugInInterfaceID,
  946. &cfPluginInterface,
  947. &score);
  948. if (kernResult != kIOReturnSuccess)
  949. {
  950. error = [NSError errorWithDomain:NSMachErrorDomain code:kernResult userInfo:nil];
  951. errorCode = 1;
  952. break;
  953. }
  954. // .. use it to get the HID interface ..
  955. hResult = (*cfPluginInterface)->QueryInterface( cfPluginInterface,
  956. CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID122),
  957. (LPVOID)&hidDeviceInterface);
  958. if ((hResult!=S_OK) || (hidDeviceInterface==NULL))
  959. {
  960. error = [NSError errorWithDomain:NSMachErrorDomain code:hResult userInfo:nil];
  961. errorCode = 2;
  962. break;
  963. }
  964. // .. then open it ..
  965. switch (_mode)
  966. {
  967. case kHIDRemoteModeShared:
  968. hResult = (*hidDeviceInterface)->open(hidDeviceInterface, kIOHIDOptionsTypeNone);
  969. break;
  970. case kHIDRemoteModeExclusive:
  971. case kHIDRemoteModeExclusiveAuto:
  972. hResult = (*hidDeviceInterface)->open(hidDeviceInterface, kIOHIDOptionsTypeSeizeDevice);
  973. break;
  974. default:
  975. goto cleanUp; // Ugh! But there are no "double breaks" available in C AFAIK ..
  976. break;
  977. }
  978. if (hResult!=S_OK)
  979. {
  980. error = [NSError errorWithDomain:NSMachErrorDomain code:hResult userInfo:nil];
  981. errorCode = 3;
  982. break;
  983. }
  984. opened = YES;
  985. // .. query the HID elements ..
  986. returnCode = (*hidDeviceInterface)->copyMatchingElements(hidDeviceInterface,
  987. NULL,
  988. &hidElements);
  989. if ((returnCode != kIOReturnSuccess) || (hidElements==NULL))
  990. {
  991. error = [NSError errorWithDomain:NSMachErrorDomain code:returnCode userInfo:nil];
  992. errorCode = 4;
  993. break;
  994. }
  995. // Setup an event queue for HID events!
  996. hidQueueInterface = (*hidDeviceInterface)->allocQueue(hidDeviceInterface);
  997. if (hidQueueInterface == NULL)
  998. {
  999. error = [NSError errorWithDomain:NSMachErrorDomain code:kIOReturnError userInfo:nil];
  1000. errorCode = 5;
  1001. break;
  1002. }
  1003. returnCode = (*hidQueueInterface)->create(hidQueueInterface, 0, 32);
  1004. if (returnCode != kIOReturnSuccess)
  1005. {
  1006. error = [NSError errorWithDomain:NSMachErrorDomain code:returnCode userInfo:nil];
  1007. errorCode = 6;
  1008. break;
  1009. }
  1010. // Setup of attributes stored for this HID device
  1011. hidAttribsDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
  1012. [NSValue valueWithPointer:(const void *)cfPluginInterface], kHIDRemoteCFPluginInterface,
  1013. [NSValue valueWithPointer:(const void *)hidDeviceInterface], kHIDRemoteHIDDeviceInterface,
  1014. [NSValue valueWithPointer:(const void *)hidQueueInterface], kHIDRemoteHIDQueueInterface,
  1015. nil];
  1016. {
  1017. UInt32 i, hidElementCnt = (UInt32)CFArrayGetCount(hidElements);
  1018. NSMutableDictionary *cookieButtonCodeLUT = [[NSMutableDictionary alloc] init];
  1019. NSMutableDictionary *cookieCount = [[NSMutableDictionary alloc] init];
  1020. if ((cookieButtonCodeLUT==nil) || (cookieCount==nil))
  1021. {
  1022. HIDRemoteReleaseNil(cookieButtonCodeLUT);
  1023. HIDRemoteReleaseNil(cookieCount);
  1024. error = [NSError errorWithDomain:NSMachErrorDomain code:kIOReturnError userInfo:nil];
  1025. errorCode = 7;
  1026. break;
  1027. }
  1028. // Analyze the HID elements and find matching elements
  1029. for (i=0;i<hidElementCnt;i++)
  1030. {
  1031. CFDictionaryRef hidDict;
  1032. NSNumber *usage, *usagePage, *cookie;
  1033. HIDRemoteButtonCode buttonCode = kHIDRemoteButtonCodeNone;
  1034. hidDict = CFArrayGetValueAtIndex(hidElements, i);
  1035. usage = (NSNumber *) CFDictionaryGetValue(hidDict, CFSTR(kIOHIDElementUsageKey));
  1036. usagePage = (NSNumber *) CFDictionaryGetValue(hidDict, CFSTR(kIOHIDElementUsagePageKey));
  1037. cookie = (NSNumber *) CFDictionaryGetValue(hidDict, CFSTR(kIOHIDElementCookieKey));
  1038. if ((usage!=nil) && (usagePage!=nil) && (cookie!=nil))
  1039. {
  1040. // Find the button codes for the ID combos
  1041. buttonCode = [self buttonCodeForUsage:[usage unsignedIntValue] usagePage:[usagePage unsignedIntValue]];
  1042. #ifdef _HIDREMOTE_EXTENSIONS
  1043. // Debug logging code
  1044. #define _HIDREMOTE_EXTENSIONS_SECTION 3
  1045. #include "HIDRemoteAdditions.h"
  1046. #undef _HIDREMOTE_EXTENSIONS_SECTION
  1047. #endif /* _HIDREMOTE_EXTENSIONS */
  1048. // Did record match?
  1049. if (buttonCode != kHIDRemoteButtonCodeNone)
  1050. {
  1051. NSString *pairString = [[NSString alloc] initWithFormat:@"%u_%u", [usagePage unsignedIntValue], [usage unsignedIntValue]];
  1052. NSNumber *buttonCodeNumber = [[NSNumber alloc] initWithUnsignedInt:(unsigned int)buttonCode];
  1053. #ifdef _HIDREMOTE_EXTENSIONS
  1054. // Debug logging code
  1055. #define _HIDREMOTE_EXTENSIONS_SECTION 4
  1056. #include "HIDRemoteAdditions.h"
  1057. #undef _HIDREMOTE_EXTENSIONS_SECTION
  1058. #endif /* _HIDREMOTE_EXTENSIONS */
  1059. [cookieCount setObject:buttonCodeNumber forKey:pairString];
  1060. [cookieButtonCodeLUT setObject:buttonCodeNumber forKey:cookie];
  1061. (*hidQueueInterface)->addElement(hidQueueInterface,
  1062. (IOHIDElementCookie) [cookie unsignedIntValue],
  1063. 0);
  1064. #ifdef _HIDREMOTE_EXTENSIONS
  1065. // Get current Apple Remote ID value
  1066. #define _HIDREMOTE_EXTENSIONS_SECTION 7
  1067. #include "HIDRemoteAdditions.h"
  1068. #undef _HIDREMOTE_EXTENSIONS_SECTION
  1069. #endif /* _HIDREMOTE_EXTENSIONS */
  1070. HIDRemoteRelease(buttonCodeNumber);
  1071. HIDRemoteRelease(pairString);
  1072. }
  1073. }
  1074. }
  1075. // Compare number of *unique* matches (thus the cookieCount dictionary) with required minimum
  1076. if ([cookieCount count] < 10)
  1077. {
  1078. HIDRemoteReleaseNil(cookieButtonCodeLUT);
  1079. HIDRemoteReleaseNil(cookieCount);
  1080. error = [NSError errorWithDomain:NSMachErrorDomain code:kIOReturnError userInfo:nil];
  1081. errorCode = 8;
  1082. break;
  1083. }
  1084. [hidAttribsDict setObject:cookieButtonCodeLUT forKey:kHIDRemoteCookieButtonCodeLUT];
  1085. HIDRemoteReleaseNil(cookieButtonCodeLUT);
  1086. HIDRemoteReleaseNil(cookieCount);
  1087. }
  1088. // Finish setup of IOHIDQueueInterface with CFRunLoop
  1089. returnCode = (*hidQueueInterface)->createAsyncEventSource(hidQueueInterface, &queueEventSource);
  1090. if ((returnCode != kIOReturnSuccess) || (queueEventSource == NULL))
  1091. {
  1092. error = [NSError errorWithDomain:NSMachErrorDomain code:returnCode userInfo:nil];
  1093. errorCode = 9;
  1094. break;
  1095. }
  1096. returnCode = (*hidQueueInterface)->setEventCallout(hidQueueInterface, HIDEventCallback, (void *)((intptr_t)service), (__HIDRemoteBridge void *)self);
  1097. if (returnCode != kIOReturnSuccess)
  1098. {
  1099. error = [NSError errorWithDomain:NSMachErrorDomain code:returnCode userInfo:nil];
  1100. errorCode = 10;
  1101. break;
  1102. }
  1103. CFRunLoopAddSource( CFRunLoopGetCurrent(),
  1104. queueEventSource,
  1105. kCFRunLoopCommonModes);
  1106. [hidAttribsDict setObject:[NSValue valueWithPointer:(const void *)queueEventSource] forKey:kHIDRemoteCFRunLoopSource];
  1107. returnCode = (*hidQueueInterface)->start(hidQueueInterface);
  1108. if (returnCode != kIOReturnSuccess)
  1109. {
  1110. error = [NSError errorWithDomain:NSMachErrorDomain code:returnCode userInfo:nil];
  1111. errorCode = 11;
  1112. break;
  1113. }
  1114. queueStarted = YES;
  1115. // Setup device notifications
  1116. returnCode = IOServiceAddInterestNotification( _notifyPort,
  1117. service,
  1118. kIOGeneralInterest,
  1119. ServiceNotificationCallback,
  1120. (__HIDRemoteBridge void *)(self),
  1121. &serviceNotification);
  1122. if ((returnCode != kIOReturnSuccess) || (serviceNotification==0))
  1123. {
  1124. error = [NSError errorWithDomain:NSMachErrorDomain code:returnCode userInfo:nil];
  1125. errorCode = 12;
  1126. break;
  1127. }
  1128. [hidAttribsDict setObject:[NSNumber numberWithUnsignedInt:(unsigned int)serviceNotification] forKey:kHIDRemoteServiceNotification];
  1129. // Retain service
  1130. if (IOObjectRetain(service) != kIOReturnSuccess)
  1131. {
  1132. error = [NSError errorWithDomain:NSMachErrorDomain code:kIOReturnError userInfo:nil];
  1133. errorCode = 13;
  1134. break;
  1135. }
  1136. [hidAttribsDict setObject:[NSNumber numberWithUnsignedInt:(unsigned int)service] forKey:kHIDRemoteService];
  1137. // Get some (somewhat optional) infos on the device
  1138. {
  1139. CFStringRef product, manufacturer, transport;
  1140. if ((product = IORegistryEntryCreateCFProperty( (io_registry_entry_t)service,
  1141. (CFStringRef) @"Product",
  1142. kCFAllocatorDefault,
  1143. 0)) != NULL)
  1144. {
  1145. if (CFGetTypeID(product) == CFStringGetTypeID())
  1146. {
  1147. [hidAttribsDict setObject:(__HIDRemoteBridge NSString *)product forKey:kHIDRemoteProduct];
  1148. }
  1149. CFRelease(product);
  1150. }
  1151. if ((manufacturer = IORegistryEntryCreateCFProperty( (io_registry_entry_t)service,
  1152. (CFStringRef) @"Manufacturer",
  1153. kCFAllocatorDefault,
  1154. 0)) != NULL)
  1155. {
  1156. if (CFGetTypeID(manufacturer) == CFStringGetTypeID())
  1157. {
  1158. [hidAttribsDict setObject:(__HIDRemoteBridge NSString *)manufacturer forKey:kHIDRemoteManufacturer];
  1159. }
  1160. CFRelease(manufacturer);
  1161. }
  1162. if ((transport = IORegistryEntryCreateCFProperty( (io_registry_entry_t)service,
  1163. (CFStringRef) @"Transport",
  1164. kCFAllocatorDefault,
  1165. 0)) != NULL)
  1166. {
  1167. if (CFGetTypeID(transport) == CFStringGetTypeID())
  1168. {
  1169. [hidAttribsDict setObject:(__HIDRemoteBridge NSString *)transport forKey:kHIDRemoteTransport];
  1170. }
  1171. CFRelease(transport);
  1172. }
  1173. }
  1174. // Determine Aluminum Remote support
  1175. {
  1176. CFNumberRef aluSupport;
  1177. HIDRemoteAluminumRemoteSupportLevel supportLevel = kHIDRemoteAluminumRemoteSupportLevelNone;
  1178. if ((_mode == kHIDRemoteModeExclusive) || (_mode == kHIDRemoteModeExclusiveAuto))
  1179. {
  1180. // Determine if this driver offers on-demand support for the Aluminum Remote (only relevant under OS versions < 10.6.2)
  1181. if ((aluSupport = IORegistryEntryCreateCFProperty((io_registry_entry_t)service,
  1182. (CFStringRef) @"AluminumRemoteSupportLevelOnDemand",
  1183. kCFAllocatorDefault,
  1184. 0)) != nil)
  1185. {
  1186. // There is => request the driver to enable it for us
  1187. if (IORegistryEntrySetCFProperty((io_registry_entry_t)service,
  1188. CFSTR("EnableAluminumRemoteSupportForMe"),
  1189. (__HIDRemoteBridge CFTypeRef)([NSDictionary dictionaryWithObjectsAndKeys:
  1190. [NSNumber numberWithLongLong:(long long)getpid()], @"pid",
  1191. [NSNumber numberWithLongLong:(long long)getuid()], @"uid",
  1192. nil])) == kIOReturnSuccess)
  1193. {
  1194. if (CFGetTypeID(aluSupport) == CFNumberGetTypeID())
  1195. {
  1196. supportLevel = (HIDRemoteAluminumRemoteSupportLevel) [(__HIDRemoteBridge NSNumber *)aluSupport intValue];
  1197. }
  1198. [hidAttribsDict setObject:[NSNumber numberWithBool:YES] forKey:kHIDRemoteAluminumRemoteSupportOnDemand];
  1199. }
  1200. CFRelease(aluSupport);
  1201. }
  1202. }
  1203. if (supportLevel == kHIDRemoteAluminumRemoteSupportLevelNone)
  1204. {
  1205. if ((aluSupport = IORegistryEntryCreateCFProperty((io_registry_entry_t)service,
  1206. (CFStringRef) @"AluminumRemoteSupportLevel",
  1207. kCFAllocatorDefault,
  1208. 0)) != nil)
  1209. {
  1210. if (CFGetTypeID(aluSupport) == CFNumberGetTypeID())
  1211. {
  1212. supportLevel = (HIDRemoteAluminumRemoteSupportLevel) [(__HIDRemoteBridge NSNumber *)aluSupport intValue];
  1213. }
  1214. CFRelease(aluSupport);
  1215. }
  1216. else
  1217. {
  1218. CFStringRef ioKitClassName;
  1219. if ((ioKitClassName = IORegistryEntryCreateCFProperty( (io_registry_entry_t)service,
  1220. CFSTR(kIOClassKey),
  1221. kCFAllocatorDefault,
  1222. 0)) != nil)
  1223. {
  1224. if ([(__HIDRemoteBridge NSString *)ioKitClassName isEqual:@"AppleIRController"])
  1225. {
  1226. if ([HIDRemote OSXVersion] >= 0x1062)
  1227. {
  1228. // Support for the Aluminum Remote was added only with OS 10.6.2. Previous versions can not distinguish
  1229. // between the Center and the new, seperate Play/Pause button. They'll recognize both as presses of the
  1230. // "Center" button.
  1231. //
  1232. // You CAN, however, receive Aluminum Remote button presses even under OS 10.5 when using Remote Buddy's
  1233. // Virtual Remote. While Remote Buddy does support the Aluminum Remote across all OS releases it runs on,
  1234. // its Virtual Remote can only emulate Aluminum Remote button presses under OS 10.5 and up in order not to
  1235. // break compatibility with applications whose IR Remote code relies on driver internals. [13-Nov-09]
  1236. supportLevel = kHIDRemoteAluminumRemoteSupportLevelNative;
  1237. }
  1238. }
  1239. CFRelease(ioKitClassName);
  1240. }
  1241. }
  1242. }
  1243. [hidAttribsDict setObject:(NSNumber *)[NSNumber numberWithInt:(int)supportLevel] forKey:kHIDRemoteAluminumRemoteSupportLevel];
  1244. }
  1245. // Add it to the serviceAttribMap
  1246. [_serviceAttribMap setObject:hidAttribsDict forKey:[NSNumber numberWithUnsignedInt:(unsigned int)service]];
  1247. // And we're done with setup ..
  1248. if (([self delegate]!=nil) &&
  1249. ([[self delegate] respondsToSelector:@selector(hidRemote:foundNewHardwareWithAttributes:)]))
  1250. {
  1251. [((NSObject <HIDRemoteDelegate> *)[self delegate]) hidRemote:self foundNewHardwareWithAttributes:hidAttribsDict];
  1252. }
  1253. HIDRemoteReleaseNil(hidAttribsDict);
  1254. return(YES);
  1255. }while(0);
  1256. cleanUp:
  1257. if (([self delegate]!=nil) &&
  1258. ([[self delegate] respondsToSelector:@selector(hidRemote:failedNewHardwareWithError:)]))
  1259. {
  1260. if (error!=nil)
  1261. {
  1262. error = [NSError errorWithDomain:[error domain]
  1263. code:[error code]
  1264. userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:errorCode] forKey:@"InternalErrorCode"]
  1265. ];
  1266. }
  1267. [((NSObject <HIDRemoteDelegate> *)[self delegate]) hidRemote:self failedNewHardwareWithError:error];
  1268. }
  1269. // An error occured or this device is not of interest .. cleanup ..
  1270. if (serviceNotification!=0)
  1271. {
  1272. IOObjectRelease(serviceNotification);
  1273. serviceNotification = 0;
  1274. }
  1275. if (queueEventSource!=NULL)
  1276. {
  1277. CFRunLoopSourceInvalidate(queueEventSource);
  1278. queueEventSource=NULL;
  1279. }
  1280. if (hidQueueInterface!=NULL)
  1281. {
  1282. if (queueStarted)
  1283. {
  1284. (*hidQueueInterface)->stop(hidQueueInterface);
  1285. }
  1286. (*hidQueueInterface)->dispose(hidQueueInterface);
  1287. (*hidQueueInterface)->Release(hidQueueInterface);
  1288. hidQueueInterface = NULL;
  1289. }
  1290. if (hidAttribsDict!=nil)
  1291. {
  1292. HIDRemoteReleaseNil(hidAttribsDict);
  1293. }
  1294. if (hidElements!=NULL)
  1295. {
  1296. CFRelease(hidElements);
  1297. hidElements = NULL;
  1298. }
  1299. if (hidDeviceInterface!=NULL)
  1300. {
  1301. if (opened)
  1302. {
  1303. (*hidDeviceInterface)->close(hidDeviceInterface);
  1304. }
  1305. (*hidDeviceInterface)->Release(hidDeviceInterface);
  1306. // opened = NO;
  1307. hidDeviceInterface = NULL;
  1308. }
  1309. if (cfPluginInterface!=NULL)
  1310. {
  1311. IODestroyPlugInInterface(cfPluginInterface);
  1312. cfPluginInterface = NULL;
  1313. }
  1314. return (NO);
  1315. }
  1316. - (void)_destructService:(io_object_t)service
  1317. {
  1318. NSNumber *serviceValue;
  1319. NSMutableDictionary *serviceDict = NULL;
  1320. if ((serviceValue = [NSNumber numberWithUnsignedInt:(unsigned int)service]) == nil)
  1321. {
  1322. return;
  1323. }
  1324. serviceDict = [_serviceAttribMap objectForKey:serviceValue];
  1325. if (serviceDict!=nil)
  1326. {
  1327. IOHIDDeviceInterface122 **hidDeviceInterface = NULL;
  1328. IOCFPlugInInterface **cfPluginInterface = NULL;
  1329. IOHIDQueueInterface **hidQueueInterface = NULL;
  1330. io_object_t serviceNotification = 0;
  1331. CFRunLoopSourceRef queueEventSource = NULL;
  1332. io_object_t theService = 0;
  1333. NSMutableDictionary *cookieButtonMap = nil;
  1334. NSTimer *simulateHoldTimer = nil;
  1335. serviceNotification = (io_object_t) ([serviceDict objectForKey:kHIDRemoteServiceNotification] ? [[serviceDict objectForKey:kHIDRemoteServiceNotification] unsignedIntValue] : 0);
  1336. theService = (io_object_t) ([serviceDict objectForKey:kHIDRemoteService] ? [[serviceDict objectForKey:kHIDRemoteService] unsignedIntValue] : 0);
  1337. queueEventSource = (CFRunLoopSourceRef) ([serviceDict objectForKey:kHIDRemoteCFRunLoopSource] ? [[serviceDict objectForKey:kHIDRemoteCFRunLoopSource] pointerValue] : NULL);
  1338. hidQueueInterface = (IOHIDQueueInterface **) ([serviceDict objectForKey:kHIDRemoteHIDQueueInterface] ? [[serviceDict objectForKey:kHIDRemoteHIDQueueInterface] pointerValue] : NULL);
  1339. hidDeviceInterface = (IOHIDDeviceInterface122 **) ([serviceDict objectForKey:kHIDRemoteHIDDeviceInterface] ? [[serviceDict objectForKey:kHIDRemoteHIDDeviceInterface] pointerValue] : NULL);
  1340. cfPluginInterface = (IOCFPlugInInterface **) ([serviceDict objectForKey:kHIDRemoteCFPluginInterface] ? [[serviceDict objectForKey:kHIDRemoteCFPluginInterface] pointerValue] : NULL);
  1341. cookieButtonMap = (NSMutableDictionary *) [serviceDict objectForKey:kHIDRemoteCookieButtonCodeLUT];
  1342. simulateHoldTimer = (NSTimer *) [serviceDict objectForKey:kHIDRemoteSimulateHoldEventsTimer];
  1343. HIDRemoteRetain(serviceDict);
  1344. [_serviceAttribMap removeObjectForKey:serviceValue];
  1345. if (([serviceDict objectForKey:kHIDRemoteAluminumRemoteSupportOnDemand]!=nil) && [[serviceDict objectForKey:kHIDRemoteAluminumRemoteSupportOnDemand] boolValue] && (theService != 0))
  1346. {
  1347. // We previously requested the driver to enable Aluminum Remote support for us. Tell it to turn it off again - now that we no longer need it
  1348. IORegistryEntrySetCFProperty( (io_registry_entry_t)theService,
  1349. CFSTR("DisableAluminumRemoteSupportForMe"),
  1350. (__HIDRemoteBridge CFTypeRef)([NSDictionary dictionaryWithObjectsAndKeys:
  1351. [NSNumber numberWithLongLong:(long long)getpid()], @"pid",
  1352. [NSNumber numberWithLongLong:(long long)getuid()], @"uid",
  1353. nil]));
  1354. }
  1355. if (([self delegate]!=nil) &&
  1356. ([[self delegate] respondsToSelector:@selector(hidRemote:releasedHardwareWithAttributes:)]))
  1357. {
  1358. [((NSObject <HIDRemoteDelegate> *)[self delegate]) hidRemote:self releasedHardwareWithAttributes:serviceDict];
  1359. }
  1360. if (simulateHoldTimer!=nil)
  1361. {
  1362. [simulateHoldTimer invalidate];
  1363. }
  1364. if (serviceNotification!=0)
  1365. {
  1366. IOObjectRelease(serviceNotification);
  1367. }
  1368. if (queueEventSource!=NULL)
  1369. {
  1370. CFRunLoopRemoveSource( CFRunLoopGetCurrent(),
  1371. queueEventSource,
  1372. kCFRunLoopCommonModes);
  1373. }
  1374. if ((hidQueueInterface!=NULL) && (cookieButtonMap!=nil))
  1375. {
  1376. NSEnumerator *cookieEnum = [cookieButtonMap keyEnumerator];
  1377. NSNumber *cookie;
  1378. while ((cookie = [cookieEnum nextObject]) != nil)
  1379. {
  1380. if ((*hidQueueInterface)->hasElement(hidQueueInterface, (IOHIDElementCookie) [cookie unsignedIntValue]))
  1381. {
  1382. (*hidQueueInterface)->removeElement(hidQueueInterface,
  1383. (IOHIDElementCookie) [cookie unsignedIntValue]);
  1384. }
  1385. };
  1386. }
  1387. if (hidQueueInterface!=NULL)
  1388. {
  1389. (*hidQueueInterface)->stop(hidQueueInterface);
  1390. (*hidQueueInterface)->dispose(hidQueueInterface);
  1391. (*hidQueueInterface)->Release(hidQueueInterface);
  1392. }
  1393. if (hidDeviceInterface!=NULL)
  1394. {
  1395. (*hidDeviceInterface)->close(hidDeviceInterface);
  1396. (*hidDeviceInterface)->Release(hidDeviceInterface);
  1397. }
  1398. if (cfPluginInterface!=NULL)
  1399. {
  1400. IODestroyPlugInInterface(cfPluginInterface);
  1401. }
  1402. if (theService!=0)
  1403. {
  1404. IOObjectRelease(theService);
  1405. }
  1406. HIDRemoteRelease(serviceDict);
  1407. }
  1408. }
  1409. #pragma mark - PRIVATE: HID Event handling
  1410. - (void)_simulateHoldEvent:(NSTimer *)aTimer
  1411. {
  1412. NSMutableDictionary *hidAttribsDict;
  1413. NSTimer *shTimer;
  1414. NSNumber *shButtonCode;
  1415. if ((hidAttribsDict = (NSMutableDictionary *)[aTimer userInfo]) != nil)
  1416. {
  1417. if (((shTimer = [hidAttribsDict objectForKey:kHIDRemoteSimulateHoldEventsTimer]) != nil) &&
  1418. ((shButtonCode = [hidAttribsDict objectForKey:kHIDRemoteSimulateHoldEventsOriginButtonCode]) != nil))
  1419. {
  1420. [shTimer invalidate];
  1421. [hidAttribsDict removeObjectForKey:kHIDRemoteSimulateHoldEventsTimer];
  1422. [self _sendButtonCode:(((HIDRemoteButtonCode)[shButtonCode unsignedIntValue])|kHIDRemoteButtonCodeHoldMask) isPressed:YES hidAttribsDict:hidAttribsDict];
  1423. }
  1424. }
  1425. }
  1426. - (void)_handleButtonCode:(HIDRemoteButtonCode)buttonCode isPressed:(BOOL)isPressed hidAttribsDict:(NSMutableDictionary *)hidAttribsDict
  1427. {
  1428. switch (buttonCode)
  1429. {
  1430. case kHIDRemoteButtonCodeIDChanged:
  1431. // Do nothing, this is handled seperately
  1432. break;
  1433. case kHIDRemoteButtonCodeUp:
  1434. case kHIDRemoteButtonCodeDown:
  1435. if (_simulateHoldEvents)
  1436. {
  1437. NSTimer *shTimer = nil;
  1438. NSNumber *shButtonCode = nil;
  1439. [[hidAttribsDict objectForKey:kHIDRemoteSimulateHoldEventsTimer] invalidate];
  1440. if (isPressed)
  1441. {
  1442. [hidAttribsDict setObject:[NSNumber numberWithUnsignedInt:buttonCode] forKey:kHIDRemoteSimulateHoldEventsOriginButtonCode];
  1443. if ((shTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:0.7] interval:0.1 target:self selector:@selector(_simulateHoldEvent:) userInfo:hidAttribsDict repeats:NO]) != nil)
  1444. {
  1445. [hidAttribsDict setObject:shTimer forKey:kHIDRemoteSimulateHoldEventsTimer];
  1446. // Using CFRunLoopAddTimer instead of [[NSRunLoop currentRunLoop] addTimer:.. for consistency with run loop modes.
  1447. // The kCFRunLoopCommonModes counterpart NSRunLoopCommonModes is only available in 10.5 and later, whereas this code
  1448. // is designed to be also compatible with 10.4. CFRunLoopTimerRef is "toll-free-bridged" with NSTimer since 10.0.
  1449. CFRunLoopAddTimer(CFRunLoopGetCurrent(), (CFRunLoopTimerRef)shTimer, kCFRunLoopCommonModes);
  1450. HIDRemoteRelease(shTimer);
  1451. break;
  1452. }
  1453. }
  1454. else
  1455. {
  1456. shTimer = [hidAttribsDict objectForKey:kHIDRemoteSimulateHoldEventsTimer];
  1457. shButtonCode = [hidAttribsDict objectForKey:kHIDRemoteSimulateHoldEventsOriginButtonCode];
  1458. if ((shTimer!=nil) && (shButtonCode!=nil))
  1459. {
  1460. [self _sendButtonCode:(HIDRemoteButtonCode)[shButtonCode unsignedIntValue] isPressed:YES hidAttribsDict:hidAttribsDict];
  1461. [self _sendButtonCode:(HIDRemoteButtonCode)[shButtonCode unsignedIntValue] isPressed:NO hidAttribsDict:hidAttribsDict];
  1462. }
  1463. else
  1464. {
  1465. if (shButtonCode!=nil)
  1466. {
  1467. [self _sendButtonCode:(((HIDRemoteButtonCode)[shButtonCode unsignedIntValue])|kHIDRemoteButtonCodeHoldMask) isPressed:NO hidAttribsDict:hidAttribsDict];
  1468. }
  1469. }
  1470. }
  1471. [hidAttribsDict removeObjectForKey:kHIDRemoteSimulateHoldEventsTimer];
  1472. [hidAttribsDict removeObjectForKey:kHIDRemoteSimulateHoldEventsOriginButtonCode];
  1473. break;
  1474. }
  1475. default:
  1476. [self _sendButtonCode:buttonCode isPressed:isPressed hidAttribsDict:hidAttribsDict];
  1477. break;
  1478. }
  1479. }
  1480. - (void)_sendButtonCode:(HIDRemoteButtonCode)buttonCode isPressed:(BOOL)isPressed hidAttribsDict:(NSMutableDictionary *)hidAttribsDict
  1481. {
  1482. if (([self delegate]!=nil) &&
  1483. ([[self delegate] respondsToSelector:@selector(hidRemote:eventWithButton:isPressed:fromHardwareWithAttributes:)]))
  1484. {
  1485. switch (buttonCode & (~kHIDRemoteButtonCodeAluminumMask))
  1486. {
  1487. case kHIDRemoteButtonCodePlay:
  1488. case kHIDRemoteButtonCodeCenter:
  1489. if (buttonCode & kHIDRemoteButtonCodeAluminumMask)
  1490. {
  1491. _lastSeenModel = kHIDRemoteModelAluminum;
  1492. _lastSeenModelRemoteID = _lastSeenRemoteID;
  1493. }
  1494. else
  1495. {
  1496. switch ((HIDRemoteAluminumRemoteSupportLevel)[[hidAttribsDict objectForKey:kHIDRemoteAluminumRemoteSupportLevel] intValue])
  1497. {
  1498. case kHIDRemoteAluminumRemoteSupportLevelNone:
  1499. case kHIDRemoteAluminumRemoteSupportLevelEmulation:
  1500. // Remote type can't be determined by just the Center button press
  1501. break;
  1502. case kHIDRemoteAluminumRemoteSupportLevelNative:
  1503. // Remote type can be safely determined by just the Center button press
  1504. if (((_lastSeenModel == kHIDRemoteModelAluminum) && (_lastSeenModelRemoteID != _lastSeenRemoteID)) ||
  1505. (_lastSeenModel == kHIDRemoteModelUndetermined))
  1506. {
  1507. _lastSeenModel = kHIDRemoteModelWhitePlastic;
  1508. }
  1509. break;
  1510. }
  1511. }
  1512. break;
  1513. }
  1514. // As soon as we have received a code that's unique to the Aluminum Remote, we can tell kHIDRemoteButtonCodePlayHold and kHIDRemoteButtonCodeCenterHold apart.
  1515. // Prior to that, a long press of the new "Play" button will be submitted as a "kHIDRemoteButtonCodeCenterHold", not a "kHIDRemoteButtonCodePlayHold" code.
  1516. if ((buttonCode == kHIDRemoteButtonCodeCenterHold) && (_lastSeenModel == kHIDRemoteModelAluminum))
  1517. {
  1518. buttonCode = kHIDRemoteButtonCodePlayHold;
  1519. }
  1520. [((NSObject <HIDRemoteDelegate> *)[self delegate]) hidRemote:self eventWithButton:(buttonCode & (~kHIDRemoteButtonCodeAluminumMask)) isPressed:isPressed fromHardwareWithAttributes:hidAttribsDict];
  1521. }
  1522. }
  1523. - (void)_hidEventFor:(io_service_t)hidDevice from:(IOHIDQueueInterface **)interface withResult:(IOReturn)result
  1524. {
  1525. NSMutableDictionary *hidAttribsDict = HIDRemoteAutoreleased(HIDRemoteRetained([_serviceAttribMap objectForKey:[NSNumber numberWithUnsignedInt:(unsigned int)hidDevice]]));
  1526. if (hidAttribsDict!=nil)
  1527. {
  1528. IOHIDQueueInterface **queueInterface = NULL;
  1529. queueInterface = [[hidAttribsDict objectForKey:kHIDRemoteHIDQueueInterface] pointerValue];
  1530. if (interface == queueInterface)
  1531. {
  1532. NSNumber *lastButtonPressedNumber = nil;
  1533. HIDRemoteButtonCode lastButtonPressed = kHIDRemoteButtonCodeNone;
  1534. NSMutableDictionary *cookieButtonMap = nil;
  1535. cookieButtonMap = [hidAttribsDict objectForKey:kHIDRemoteCookieButtonCodeLUT];
  1536. if ((lastButtonPressedNumber = [hidAttribsDict objectForKey:kHIDRemoteLastButtonPressed]) != nil)
  1537. {
  1538. lastButtonPressed = [lastButtonPressedNumber unsignedIntValue];
  1539. }
  1540. while (result == kIOReturnSuccess)
  1541. {
  1542. IOHIDEventStruct hidEvent;
  1543. AbsoluteTime supportedTime = { 0,0 };
  1544. result = (*queueInterface)->getNextEvent( queueInterface,
  1545. &hidEvent,
  1546. supportedTime,
  1547. 0);
  1548. if (result == kIOReturnSuccess)
  1549. {
  1550. NSNumber *buttonCodeNumber = [cookieButtonMap objectForKey:[NSNumber numberWithUnsignedInt:(unsigned int) hidEvent.elementCookie]];
  1551. #ifdef _HIDREMOTE_EXTENSIONS
  1552. // Debug logging code
  1553. #define _HIDREMOTE_EXTENSIONS_SECTION 5
  1554. #include "HIDRemoteAdditions.h"
  1555. #undef _HIDREMOTE_EXTENSIONS_SECTION
  1556. #endif /* _HIDREMOTE_EXTENSIONS */
  1557. if (buttonCodeNumber!=nil)
  1558. {
  1559. HIDRemoteButtonCode buttonCode = [buttonCodeNumber unsignedIntValue];
  1560. if (hidEvent.value == 0)
  1561. {
  1562. if (buttonCode == lastButtonPressed)
  1563. {
  1564. [self _handleButtonCode:lastButtonPressed isPressed:NO hidAttribsDict:hidAttribsDict];
  1565. lastButtonPressed = kHIDRemoteButtonCodeNone;
  1566. }
  1567. }
  1568. if (hidEvent.value != 0)
  1569. {
  1570. if (lastButtonPressed != kHIDRemoteButtonCodeNone)
  1571. {
  1572. [self _handleButtonCode:lastButtonPressed isPressed:NO hidAttribsDict:hidAttribsDict];
  1573. // lastButtonPressed = kHIDRemoteButtonCodeNone;
  1574. }
  1575. if (buttonCode == kHIDRemoteButtonCodeIDChanged)
  1576. {
  1577. if (([self delegate]!=nil) &&
  1578. ([[self delegate] respondsToSelector:@selector(hidRemote:remoteIDChangedOldID:newID:forHardwareWithAttributes:)]))
  1579. {
  1580. [((NSObject <HIDRemoteDelegate> *)[self delegate]) hidRemote:self remoteIDChangedOldID:_lastSeenRemoteID newID:hidEvent.value forHardwareWithAttributes:hidAttribsDict];
  1581. }
  1582. _lastSeenRemoteID = hidEvent.value;
  1583. _lastSeenModel = kHIDRemoteModelUndetermined;
  1584. }
  1585. [self _handleButtonCode:buttonCode isPressed:YES hidAttribsDict:hidAttribsDict];
  1586. lastButtonPressed = buttonCode;
  1587. }
  1588. }
  1589. }
  1590. };
  1591. [hidAttribsDict setObject:[NSNumber numberWithUnsignedInt:lastButtonPressed] forKey:kHIDRemoteLastButtonPressed];
  1592. }
  1593. #ifdef _HIDREMOTE_EXTENSIONS
  1594. // Debug logging code
  1595. #define _HIDREMOTE_EXTENSIONS_SECTION 6
  1596. #include "HIDRemoteAdditions.h"
  1597. #undef _HIDREMOTE_EXTENSIONS_SECTION
  1598. #endif /* _HIDREMOTE_EXTENSIONS */
  1599. }
  1600. }
  1601. #pragma mark - PRIVATE: Notification handling
  1602. - (void)_serviceMatching:(io_iterator_t)iterator
  1603. {
  1604. io_object_t matchingService = 0;
  1605. while ((matchingService = IOIteratorNext(iterator)) != 0)
  1606. {
  1607. [self _setupService:matchingService];
  1608. IOObjectRelease(matchingService);
  1609. };
  1610. }
  1611. - (void)_serviceNotificationFor:(io_service_t)service messageType:(natural_t)messageType messageArgument:(void *)messageArgument
  1612. {
  1613. if (messageType == kIOMessageServiceIsTerminated)
  1614. {
  1615. [self _destructService:service];
  1616. }
  1617. }
  1618. - (void)_updateSessionInformation
  1619. {
  1620. NSArray *consoleUsersArray;
  1621. io_service_t rootService;
  1622. if (_masterPort==0) { return; }
  1623. if ((rootService = IORegistryGetRootEntry(_masterPort)) != 0)
  1624. {
  1625. if ((consoleUsersArray = (__HIDRemoteBridge NSArray *)IORegistryEntryCreateCFProperty((io_registry_entry_t)rootService, CFSTR("IOConsoleUsers"), kCFAllocatorDefault, 0)) != nil)
  1626. {
  1627. if ([consoleUsersArray isKindOfClass:[NSArray class]]) // Be careful - ensure this really is an array
  1628. {
  1629. NSEnumerator *consoleUsersEnum; // I *love* Obj-C2's fast enumerators, but we need to stay compatible with 10.4 :-/
  1630. if ((consoleUsersEnum = [consoleUsersArray objectEnumerator]) != nil)
  1631. {
  1632. UInt64 secureEventInputPIDSum = 0;
  1633. uid_t frontUserSession = 0;
  1634. BOOL screenIsLocked = NO;
  1635. NSDictionary *consoleUserDict;
  1636. while ((consoleUserDict = [consoleUsersEnum nextObject]) != nil)
  1637. {
  1638. if ([consoleUserDict isKindOfClass:[NSDictionary class]]) // Be careful - ensure this really is a dictionary
  1639. {
  1640. NSNumber *secureInputPID;
  1641. NSNumber *onConsole;
  1642. NSNumber *userID;
  1643. NSNumber *screenIsLockedBool;
  1644. if ((secureInputPID = [consoleUserDict objectForKey:@"kCGSSessionSecureInputPID"]) != nil)
  1645. {
  1646. if ([secureInputPID isKindOfClass:[NSNumber class]])
  1647. {
  1648. secureEventInputPIDSum += ((UInt64) [secureInputPID intValue]);
  1649. }
  1650. }
  1651. if (((onConsole = [consoleUserDict objectForKey:@"kCGSSessionOnConsoleKey"]) != nil) &&
  1652. ((userID = [consoleUserDict objectForKey:@"kCGSSessionUserIDKey"]) != nil))
  1653. {
  1654. if ([onConsole isKindOfClass:[NSNumber class]] && [userID isKindOfClass:[NSNumber class]])
  1655. {
  1656. if ([onConsole boolValue])
  1657. {
  1658. frontUserSession = (uid_t) [userID intValue];
  1659. }
  1660. }
  1661. }
  1662. if ((screenIsLockedBool = [consoleUserDict objectForKey:@"CGSSessionScreenIsLocked"]) != nil)
  1663. {
  1664. if ([screenIsLockedBool isKindOfClass:[NSNumber class]])
  1665. {
  1666. screenIsLocked = [screenIsLockedBool boolValue];
  1667. }
  1668. }
  1669. }
  1670. }
  1671. _lastSecureEventInputPIDSum = secureEventInputPIDSum;
  1672. _lastFrontUserSession = frontUserSession;
  1673. _lastScreenIsLocked = screenIsLocked;
  1674. }
  1675. }
  1676. CFRelease((CFTypeRef)consoleUsersArray);
  1677. }
  1678. IOObjectRelease((io_object_t) rootService);
  1679. }
  1680. }
  1681. - (void)_silentRestart
  1682. {
  1683. if ((_mode == kHIDRemoteModeExclusive) || (_mode == kHIDRemoteModeExclusiveAuto))
  1684. {
  1685. HIDRemoteMode restartInMode = _mode;
  1686. unsigned checkActiveRemoteControlCount = [self activeRemoteControlCount];
  1687. // Only restart when we already have active remote controls - to avoid race conditions with other applications using kHIDRemoteModeExclusive mode (new in V1.2.1)
  1688. if (checkActiveRemoteControlCount > 0)
  1689. {
  1690. _isRestarting = YES;
  1691. [self stopRemoteControl];
  1692. [self startRemoteControl:restartInMode];
  1693. _isRestarting = NO;
  1694. // Check whether we lost a remote control due to restarting/secure input change notification handling (new in V1.2.1)
  1695. if (checkActiveRemoteControlCount != [self activeRemoteControlCount])
  1696. {
  1697. // Log message
  1698. NSLog(@"Lost access (mode %d) to %d IR Remote Receiver(s) after handling SecureInput change notification - please quit other apps trying to use the Remote exclusively", restartInMode, checkActiveRemoteControlCount);
  1699. }
  1700. }
  1701. }
  1702. }
  1703. - (void)_secureInputNotificationFor:(io_service_t)service messageType:(natural_t)messageType messageArgument:(void *)messageArgument
  1704. {
  1705. if (messageType == kIOMessageServiceBusyStateChange)
  1706. {
  1707. UInt64 old_lastSecureEventInputPIDSum = _lastSecureEventInputPIDSum;
  1708. uid_t old_lastFrontUserSession = _lastFrontUserSession;
  1709. BOOL old_lastScreenIsLocked = _lastScreenIsLocked;
  1710. [self _updateSessionInformation];
  1711. if (((old_lastSecureEventInputPIDSum != _lastSecureEventInputPIDSum) ||
  1712. (old_lastFrontUserSession != _lastFrontUserSession) ||
  1713. (old_lastScreenIsLocked != _lastScreenIsLocked)) && _secureEventInputWorkAround)
  1714. {
  1715. [self _silentRestart];
  1716. }
  1717. }
  1718. }
  1719. - (void)_computerDidWake:(NSNotification *)aNotification
  1720. {
  1721. // Work around for a bug in 10.8, where exclusive connections may be degraded to shared connections after a sleep/wakeup cycle (credit: Paul Duggan from Galaxy Software)
  1722. #ifdef NSAppKitVersionNumber10_8
  1723. if (NSAppKitVersionNumber >= NSAppKitVersionNumber10_8)
  1724. #else
  1725. if (NSAppKitVersionNumber >= 1187)
  1726. #endif
  1727. {
  1728. #if HIDREMOTE_THREADSAFETY_HARDENED_NOTIFICATION_HANDLING
  1729. if ([self respondsToSelector:@selector(performSelector:onThread:withObject:waitUntilDone:)]) // OS X 10.5+ only
  1730. {
  1731. if ([NSThread currentThread] != _runOnThread)
  1732. {
  1733. [self performSelector:@selector(_computerDidWake:) onThread:_runOnThread withObject:aNotification waitUntilDone:NO];
  1734. return;
  1735. }
  1736. }
  1737. #endif
  1738. [self _silentRestart];
  1739. }
  1740. }
  1741. @end
  1742. #pragma mark - PRIVATE: IOKitLib Callbacks
  1743. static void HIDEventCallback( void * target,
  1744. IOReturn result,
  1745. void * refCon,
  1746. void * sender)
  1747. {
  1748. HIDRemote *hidRemote = (__HIDRemoteBridge HIDRemote *)refCon;
  1749. HIDRemoteAutoreleasePoolOpen();
  1750. [hidRemote _hidEventFor:(io_service_t)((intptr_t)target) from:(IOHIDQueueInterface**)sender withResult:(IOReturn)result];
  1751. HIDRemoteAutoreleasePoolClose();
  1752. }
  1753. static void ServiceMatchingCallback( void *refCon,
  1754. io_iterator_t iterator)
  1755. {
  1756. HIDRemote *hidRemote = (__HIDRemoteBridge HIDRemote *)refCon;
  1757. HIDRemoteAutoreleasePoolOpen();
  1758. [hidRemote _serviceMatching:iterator];
  1759. HIDRemoteAutoreleasePoolClose();
  1760. }
  1761. static void ServiceNotificationCallback(void * refCon,
  1762. io_service_t service,
  1763. natural_t messageType,
  1764. void * messageArgument)
  1765. {
  1766. HIDRemote *hidRemote = (__HIDRemoteBridge HIDRemote *)refCon;
  1767. HIDRemoteAutoreleasePoolOpen();
  1768. [hidRemote _serviceNotificationFor:service
  1769. messageType:messageType
  1770. messageArgument:messageArgument];
  1771. HIDRemoteAutoreleasePoolClose();
  1772. }
  1773. static void SecureInputNotificationCallback( void * refCon,
  1774. io_service_t service,
  1775. natural_t messageType,
  1776. void * messageArgument)
  1777. {
  1778. HIDRemote *hidRemote = (__HIDRemoteBridge HIDRemote *)refCon;
  1779. HIDRemoteAutoreleasePoolOpen();
  1780. [hidRemote _secureInputNotificationFor:service
  1781. messageType:messageType
  1782. messageArgument:messageArgument];
  1783. HIDRemoteAutoreleasePoolClose();
  1784. }
  1785. // Attribute dictionary keys
  1786. NSString *kHIDRemoteCFPluginInterface = @"CFPluginInterface";
  1787. NSString *kHIDRemoteHIDDeviceInterface = @"HIDDeviceInterface";
  1788. NSString *kHIDRemoteCookieButtonCodeLUT = @"CookieButtonCodeLUT";
  1789. NSString *kHIDRemoteHIDQueueInterface = @"HIDQueueInterface";
  1790. NSString *kHIDRemoteServiceNotification = @"ServiceNotification";
  1791. NSString *kHIDRemoteCFRunLoopSource = @"CFRunLoopSource";
  1792. NSString *kHIDRemoteLastButtonPressed = @"LastButtonPressed";
  1793. NSString *kHIDRemoteService = @"Service";
  1794. NSString *kHIDRemoteSimulateHoldEventsTimer = @"SimulateHoldEventsTimer";
  1795. NSString *kHIDRemoteSimulateHoldEventsOriginButtonCode = @"SimulateHoldEventsOriginButtonCode";
  1796. NSString *kHIDRemoteAluminumRemoteSupportLevel = @"AluminumRemoteSupportLevel";
  1797. NSString *kHIDRemoteAluminumRemoteSupportOnDemand = @"AluminumRemoteSupportLevelOnDemand";
  1798. NSString *kHIDRemoteManufacturer = @"Manufacturer";
  1799. NSString *kHIDRemoteProduct = @"Product";
  1800. NSString *kHIDRemoteTransport = @"Transport";
  1801. // Distributed notifications
  1802. NSString *kHIDRemoteDNHIDRemotePing = @"com.candelair.ping";
  1803. NSString *kHIDRemoteDNHIDRemoteRetry = @"com.candelair.retry";
  1804. NSString *kHIDRemoteDNHIDRemoteStatus = @"com.candelair.status";
  1805. NSString *kHIDRemoteDNHIDRemoteRetryGlobalObject = @"global";
  1806. // Distributed notifications userInfo keys and values
  1807. NSString *kHIDRemoteDNStatusHIDRemoteVersionKey = @"HIDRemoteVersion";
  1808. NSString *kHIDRemoteDNStatusPIDKey = @"PID";
  1809. NSString *kHIDRemoteDNStatusModeKey = @"Mode";
  1810. NSString *kHIDRemoteDNStatusUnusedButtonCodesKey = @"UnusedButtonCodes";
  1811. NSString *kHIDRemoteDNStatusActionKey = @"Action";
  1812. NSString *kHIDRemoteDNStatusRemoteControlCountKey = @"RemoteControlCount";
  1813. NSString *kHIDRemoteDNStatusReturnToPIDKey = @"ReturnToPID";
  1814. NSString *kHIDRemoteDNStatusActionStart = @"start";
  1815. NSString *kHIDRemoteDNStatusActionStop = @"stop";
  1816. NSString *kHIDRemoteDNStatusActionUpdate = @"update";
  1817. NSString *kHIDRemoteDNStatusActionNoNeed = @"noneed";