3636#include < cbang/event/Base.h>
3737#include < cbang/event/Event.h>
3838#include < cbang/os/PowerManagement.h>
39- #include < cbang/os/MacOSUtilities.h>
4039
4140#include < IOKit/IOMessage.h>
4241#include < IOKit/ps/IOPSKeys.h>
@@ -55,52 +54,54 @@ using namespace std;
5554
5655
5756static const CFTimeInterval kCFTimeIntervalMax =
58- std:: numeric_limits<CFTimeInterval>::max();
57+ numeric_limits<CFTimeInterval>::max();
5958
6059enum {
6160 kDisplayPowerUnknown = -1 ,
62- kDisplayPowerOff = 0 ,
63- kDisplayPowerDimmed = 3 ,
64- kDisplayPowerOn = 4
61+ kDisplayPowerOff = 0 ,
62+ kDisplayPowerDimmed = 3 ,
63+ kDisplayPowerOn = 4
6564};
6665
6766
6867namespace {
6968#pragma mark c callbacks
70-
7169 void consoleUserCB (SCDynamicStoreRef s, CFArrayRef keys, void *info) {
7270 OSXOSImpl::instance ().consoleUserChanged (s, keys, info);
7371 }
7472
75- void displayPowerCB (void *ctx, io_service_t service,
76- natural_t mtype, void *marg) {
73+
74+ void displayPowerCB (
75+ void *ctx, io_service_t service, natural_t mtype, void *marg) {
7776 OSXOSImpl::instance ().displayPowerChanged (ctx, service, mtype, marg);
7877 }
7978
79+
8080 void updateTimerCB (CFRunLoopTimerRef timer, void *info) {
8181 OSXOSImpl::instance ().updateTimerFired (timer, info);
8282 }
8383
84+
8485 void heartbeatTimerCB (CFRunLoopTimerRef timer, void *info) {
8586 LOG_DEBUG (5 , " heartbeat on thread " << pthread_self () <<
8687 (pthread_main_np () ? " main" : " " ));
8788 }
8889
90+
8991 void noteQuitCB (CFNotificationCenterRef center, void *observer,
9092 CFNotificationName name, const void *object,
9193 CFDictionaryRef info) {
92- std::string n = CFStringGetCStringPtr (name, kCFStringEncodingUTF8 );
93- LOG_INFO ( 3 , " Received notification " << n );
94+ LOG_INFO ( 3 , " Received notification "
95+ << CFStringGetCStringPtr (name, kCFStringEncodingUTF8 ) );
9496 OSXOSImpl::instance ().requestExit ();
9597 }
96-
9798}
9899
99100
100101OSXOSImpl *OSXOSImpl::singleton = 0 ;
101102
102103
103- OSXOSImpl::OSXOSImpl (App &app) : OS(app), consoleUser(CFSTR( " unknown" ) ) {
104+ OSXOSImpl::OSXOSImpl (App &app) : OS(app), consoleUser(" unknown" ) {
104105 if (singleton) THROW (" There can be only one OSXOSImpl" );
105106 singleton = this ;
106107 initialize ();
@@ -109,10 +110,7 @@ OSXOSImpl::OSXOSImpl(App &app) : OS(app), consoleUser(CFSTR("unknown")) {
109110
110111OSXOSImpl::~OSXOSImpl () {
111112 // Stop any update timer
112- if (updateTimer) {
113- CFRunLoopTimerInvalidate (updateTimer);
114- CFRelease (updateTimer);
115- }
113+ if (updateTimer) CFRunLoopTimerInvalidate (updateTimer);
116114
117115 // Deregister for display power changes
118116 if (displayWrangler) IOObjectRelease (displayWrangler);
@@ -122,21 +120,11 @@ OSXOSImpl::~OSXOSImpl() {
122120 kCFRunLoopDefaultMode );
123121
124122 if (displayNotePort) IONotificationPortDestroy (displayNotePort);
125-
126123 if (displayNotifier) IOObjectRelease (displayNotifier);
127124
128125 // Deregister for console user changes
129- if (consoleUserRLS) {
130- CFRunLoopSourceInvalidate (consoleUserRLS);
131- CFRelease (consoleUserRLS);
132- }
133-
134- if (consoleUserDS) {
135- SCDynamicStoreSetNotificationKeys (consoleUserDS, 0 , 0 );
136- CFRelease (consoleUserDS);
137- }
138-
139- if (consoleUser) CFRelease (consoleUser);
126+ if (consoleUserRLS) CFRunLoopSourceInvalidate (consoleUserRLS);
127+ if (consoleUserDS) SCDynamicStoreSetNotificationKeys (consoleUserDS, 0 , 0 );
140128
141129 CFNotificationCenterRef nc = CFNotificationCenterGetDarwinNotifyCenter ();
142130 CFNotificationCenterRemoveEveryObserver (nc, this );
@@ -174,18 +162,12 @@ void OSXOSImpl::addHeartbeatTimerToRunLoop(CFRunLoopRef loop) {
174162 // note this may fail silently
175163 if (!loop) return ;
176164
177- CFRunLoopTimerRef timer =
165+ MacOSRef< CFRunLoopTimerRef> timer =
178166 CFRunLoopTimerCreate (0 , 0 , 3600 , 0 , 0 , heartbeatTimerCB, 0 );
179- if (timer) {
180- CFRunLoopAddTimer (loop, timer, kCFRunLoopDefaultMode );
181- CFRelease (timer);
182- }
167+ if (timer) CFRunLoopAddTimer (loop, timer, kCFRunLoopDefaultMode );
183168}
184169
185170
186- const char *OSXOSImpl::getName () const {return " macosx" ;}
187-
188-
189171void OSXOSImpl::dispatch () {
190172 if (!pthread_main_np ())
191173 THROW (" OSXOSImpl::dispatch() must be called on main thread" );
@@ -224,8 +206,10 @@ void OSXOSImpl::finishInit() {
224206void OSXOSImpl::updateSystemIdle () {
225207 bool shouldBeIdle = displayPower == kDisplayPowerOff || loginwindowIsActive ||
226208 screensaverIsActive || screenIsLocked;
209+
227210 if (shouldBeIdle == systemIsIdle) return ;
228211 systemIsIdle = shouldBeIdle;
212+
229213 event->activate ();
230214}
231215
@@ -266,6 +250,7 @@ void OSXOSImpl::delayedUpdateSystemIdle(int delay) {
266250 } else if (delay < currentDelay) {
267251 // Use sooner of current fire date and candidateTime
268252 CFAbsoluteTime currentTime = CFRunLoopTimerGetNextFireDate (updateTimer);
253+
269254 if (candidateTime < currentTime) {
270255 CFRunLoopTimerSetNextFireDate (updateTimer, candidateTime);
271256 currentDelay = delay;
@@ -274,7 +259,7 @@ void OSXOSImpl::delayedUpdateSystemIdle(int delay) {
274259}
275260
276261
277- void OSXOSImpl::displayDidDim () {displayPower = kDisplayPowerDimmed ;}
262+ void OSXOSImpl::displayDidDim () {displayPower = kDisplayPowerDimmed ;}
278263void OSXOSImpl::displayDidUndim () {displayPower = kDisplayPowerOn ;}
279264
280265
@@ -321,17 +306,16 @@ void OSXOSImpl::displayPowerChanged(void *context, io_service_t service,
321306
322307int OSXOSImpl::getCurrentDisplayPower () {
323308 // will only return values kDisplayPowerUnknown thru kDisplayPowerOn
324- int state = kDisplayPowerUnknown ;
309+ int state = kDisplayPowerUnknown ;
325310 int maxstate = 4 ;
326311
327312 if (!displayWrangler)
328313 displayWrangler = IOServiceGetMatchingService
329314 (kIOMasterPortDefault , IOServiceNameMatching (" IODisplayWrangler" ));
330315
331316 if (displayWrangler) {
332- CFMutableDictionaryRef props = 0 ;
333-
334- auto kr = IORegistryEntryCreateCFProperties (displayWrangler, &props, 0 , 0 );
317+ MacOSRef<CFMutableDictionaryRef> props;
318+ auto kr = IORegistryEntryCreateCFProperties (displayWrangler, props, 0 , 0 );
335319
336320 if (kr == KERN_SUCCESS) {
337321 auto dict = (CFMutableDictionaryRef)CFDictionaryGetValue
@@ -357,7 +341,6 @@ int OSXOSImpl::getCurrentDisplayPower() {
357341 }
358342 }
359343
360- if (props) CFRelease (props);
361344 IOObjectRelease (displayWrangler);
362345 displayWrangler = 0 ;
363346 }
@@ -424,17 +407,14 @@ bool OSXOSImpl::registerForDisplayPowerNotifications() {
424407}
425408
426409
427- void OSXOSImpl::consoleUserChanged (SCDynamicStoreRef store,
428- CFArrayRef changedKeys, void *info) {
429- if (consoleUser) CFRelease (consoleUser);
430- consoleUser = SCDynamicStoreCopyConsoleUser (consoleUserDS, 0 , 0 );
410+ void OSXOSImpl::consoleUserChanged (
411+ SCDynamicStoreRef store, CFArrayRef changedKeys, void *info) {
412+ consoleUser = MacOSString (SCDynamicStoreCopyConsoleUser (consoleUserDS, 0 , 0 ));
431413
432- LOG_DEBUG (4 , __func__ << " () " << MacOSUtilities::toString (consoleUser) );
414+ LOG_DEBUG (4 , __func__ << " () " << (string)consoleUser );
433415
434416 bool wasActive = loginwindowIsActive;
435- loginwindowIsActive = !consoleUser || !CFStringGetLength (consoleUser) ||
436- CFEqual (consoleUser, CFSTR (" loginwindow" ));
437-
417+ loginwindowIsActive = consoleUser.empty () || consoleUser == " loginwindow" ;
438418 bool changed = wasActive != loginwindowIsActive;
439419
440420 if (changed || !store) {
@@ -450,9 +430,9 @@ bool OSXOSImpl::registerForConsoleUserNotifications() {
450430 consoleUserDS = SCDynamicStoreCreate
451431 (0 , CFSTR (" FAHClient Console User Watcher" ), consoleUserCB, &context);
452432
453- CFStringRef consoleUserKey = SCDynamicStoreKeyCreateConsoleUser (0 );
454- CFStringRef values[] = {consoleUserKey};
455- CFArrayRef keys =
433+ MacOSString consoleUserKey = SCDynamicStoreKeyCreateConsoleUser (0 );
434+ CFStringRef values[] = {(CFStringRef) consoleUserKey};
435+ MacOSRef< CFArrayRef> keys =
456436 CFArrayCreate (0 , (const void **)values, 1 , &kCFTypeArrayCallBacks );
457437
458438 bool ok = consoleUserKey && keys && consoleUserDS;
@@ -467,17 +447,12 @@ bool OSXOSImpl::registerForConsoleUserNotifications() {
467447 if (!ok) {
468448 if (consoleUserDS) {
469449 SCDynamicStoreSetNotificationKeys (consoleUserDS, 0 , 0 );
470- CFRelease (consoleUserDS);
471450 consoleUserDS = 0 ;
472451 }
473452
474- if (consoleUser) CFRelease (consoleUser);
475- consoleUser = CFSTR (" unknown" );
453+ consoleUser = " unknown" ;
476454 }
477455
478- if (consoleUserKey) CFRelease (consoleUserKey);
479- if (keys) CFRelease (keys);
480-
481456 // Initial consoleUser value will be set in finishInit
482457 return ok;
483458}
@@ -488,24 +463,16 @@ bool OSXOSImpl::registerForDarwinNotifications() {
488463
489464 if (!nc) return false ;
490465
491- std:: string user = " nobody" ;
466+ string user = " nobody" ;
492467 struct passwd *pwent = getpwuid (getuid ());
493468 if (pwent && pwent->pw_name ) user = pwent->pw_name ;
494469
495- std::string key = " org.foldingathome.fahclient." + user + " .stop" ;
496- CFStringRef name =
497- CFStringCreateWithCString (0 , key.c_str (), kCFStringEncodingUTF8 );
498-
499- if (name) {
500- CFNotificationCenterAddObserver (
501- nc, (void *)this , ¬eQuitCB, name, 0 ,
502- CFNotificationSuspensionBehaviorCoalesce);
503- CFRelease (name);
470+ MacOSString name (string (" org.foldingathome.fahclient." + user + " .stop" ));
471+ CFNotificationCenterAddObserver (
472+ nc, (void *)this , ¬eQuitCB, name, 0 ,
473+ CFNotificationSuspensionBehaviorCoalesce);
504474
505- return true ;
506- }
507-
508- return false ;
475+ return true ;
509476}
510477
511478
0 commit comments