Skip to content

Commit e88d016

Browse files
committed
macOS changes
1 parent ed4d2d0 commit e88d016

File tree

2 files changed

+55
-87
lines changed

2 files changed

+55
-87
lines changed

src/fah/client/osx/OSXOSImpl.cpp

Lines changed: 40 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
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

5756
static const CFTimeInterval kCFTimeIntervalMax =
58-
std::numeric_limits<CFTimeInterval>::max();
57+
numeric_limits<CFTimeInterval>::max();
5958

6059
enum {
6160
kDisplayPowerUnknown = -1,
62-
kDisplayPowerOff = 0,
63-
kDisplayPowerDimmed = 3,
64-
kDisplayPowerOn = 4
61+
kDisplayPowerOff = 0,
62+
kDisplayPowerDimmed = 3,
63+
kDisplayPowerOn = 4
6564
};
6665

6766

6867
namespace {
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

100101
OSXOSImpl *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

110111
OSXOSImpl::~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-
189171
void OSXOSImpl::dispatch() {
190172
if (!pthread_main_np())
191173
THROW("OSXOSImpl::dispatch() must be called on main thread");
@@ -224,8 +206,10 @@ void OSXOSImpl::finishInit() {
224206
void 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;}
278263
void OSXOSImpl::displayDidUndim() {displayPower = kDisplayPowerOn;}
279264

280265

@@ -321,17 +306,16 @@ void OSXOSImpl::displayPowerChanged(void *context, io_service_t service,
321306

322307
int 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, &noteQuitCB, name, 0,
502-
CFNotificationSuspensionBehaviorCoalesce);
503-
CFRelease(name);
470+
MacOSString name(string("org.foldingathome.fahclient." + user + ".stop"));
471+
CFNotificationCenterAddObserver(
472+
nc, (void *)this, &noteQuitCB, name, 0,
473+
CFNotificationSuspensionBehaviorCoalesce);
504474

505-
return true;
506-
}
507-
508-
return false;
475+
return true;
509476
}
510477

511478

src/fah/client/osx/OSXOSImpl.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <fah/client/OS.h>
3232

3333
#include <cbang/os/Thread.h>
34+
#include <cbang/os/MacOSUtilities.h>
3435

3536
#include <CoreFoundation/CoreFoundation.h>
3637
#include <SystemConfiguration/SystemConfiguration.h>
@@ -46,25 +47,25 @@ namespace FAH {
4647
static OSXOSImpl *singleton;
4748

4849
std::atomic<bool> systemIsIdle = false;
49-
bool screensaverIsActive = false;
50-
bool screenIsLocked = false;
51-
bool loginwindowIsActive = false;
50+
bool screensaverIsActive = false;
51+
bool screenIsLocked = false;
52+
bool loginwindowIsActive = false;
5253

5354
io_service_t displayWrangler = 0;
54-
IONotificationPortRef displayNotePort = 0;
55-
CFRunLoopSourceRef displayNoteSource = 0;
56-
io_object_t displayNotifier = 0;
55+
io_object_t displayNotifier = 0;
56+
cb::MacOSRef<IONotificationPortRef> displayNotePort;
57+
cb::MacOSRef<CFRunLoopSourceRef> displayNoteSource;
5758

58-
SCDynamicStoreRef consoleUserDS = 0;
59-
CFRunLoopSourceRef consoleUserRLS = 0;
60-
CFStringRef consoleUser = 0;
59+
cb::MacOSRef<SCDynamicStoreRef> consoleUserDS;
60+
cb::MacOSRef<CFRunLoopSourceRef> consoleUserRLS;
61+
std::string consoleUser;
6162

62-
CFRunLoopTimerRef updateTimer = 0;
63+
cb::MacOSRef<CFRunLoopTimerRef> updateTimer;
6364

64-
int idleDelay = 5;
65-
int currentDelay = 0;
65+
int idleDelay = 5;
66+
int currentDelay = 0;
6667
int idleOnLoginwindowDelay = 30;
67-
int displayPower = 0;
68+
int displayPower = 0;
6869

6970
public:
7071
OSXOSImpl(App &app);
@@ -73,7 +74,7 @@ namespace FAH {
7374
static OSXOSImpl &instance();
7475

7576
// From OS
76-
const char *getName() const;
77+
const char *getName() const {return "macosx";}
7778
bool isSystemIdle() const {return systemIsIdle;}
7879
void dispatch();
7980

0 commit comments

Comments
 (0)