What I'm trying to partially convert?
Code: Select all
/*
* BSD notifications from loginwindow indicating shutdown
*/
// kLWShutdownInitiated
// User clicked shutdown: may be aborted later
#define kLWShutdowntInitiated "com.apple.system.loginwindow.shutdownInitiated"
// kLWRestartInitiated
// User clicked restart: may be aborted later
#define kLWRestartInitiated "com.apple.system.loginwindow.restartinitiated"
// kLWLogoutCancelled
// A previously initiated shutdown, restart, or logout, has been cancelled.
#define kLWLogoutCancelled "com.apple.system.loginwindow.logoutcancelled"
// kLWLogoutPointOfNoReturn
// A previously initiated shutdown, restart, or logout has succeeded, and is
// no longer abortable by anyone. Point of no return!
#define kLWLogoutPointOfNoReturn "com.apple.system.loginwindow.logoutNoReturn"
// kLWSULogoutInitiated
// Loginwindow is beginning a sequence of 1. logout, 2. software update, 3. then restart.
#define kLWSULogoutInitiated "com.apple.system.loginwindow.sulogoutinitiated"
static void
initializeShutdownNotifications(void)
{
CFMachPortRef gNotifyMachPort = NULL;
CFRunLoopSourceRef gNotifyMachPortRLS = NULL;
mach_port_t our_port = MACH_PORT_NULL;
// Tell the kernel that we are NOT shutting down at the moment, since
// configd is just launching now.
// Why: if configd crashed with "System Shutdown" == kCFbooleanTrue, reset
// it now as the situation may no longer apply.
_setRootDomainProperty(CFSTR("System Shutdown"), kCFBooleanFalse);
/* * * * * * * * * * * * * */
// Sneak in our registration for CPU power notifications here; to piggy-back
// with the other mach port registrations for LW.
notify_register_mach_port(
kIOPMCPUPowerNotificationKey,
&our_port,
0, /* flags */
&gCPUPowerNotificationToken);
notify_register_mach_port(
kLWShutdowntInitiated,
&our_port,
NOTIFY_REUSE, /* flags */
&lwNotify.shutdown);
notify_register_mach_port(
kLWRestartInitiated,
&our_port,
NOTIFY_REUSE, /* flags */
&lwNotify.restart);
notify_register_mach_port(
kLWLogoutCancelled,
&our_port,
NOTIFY_REUSE, /* flags */
&lwNotify.cancel);
notify_register_mach_port(
kLWLogoutPointOfNoReturn,
&our_port,
NOTIFY_REUSE, /* flags */
&lwNotify.pointofnoreturn);
notify_register_mach_port(
kLWSULogoutInitiated,
&our_port,
NOTIFY_REUSE, /* flags */
&lwNotify.su);
/* * * * * * * * * * * * * */
gNotifyMachPort = _SC_CFMachPortCreateWithPort(
"PowerManagement/shutdown",
our_port,
lwShutdownCallback,
NULL);
if (gNotifyMachPort) {
gNotifyMachPortRLS = CFMachPortCreateRunLoopSource(0, gNotifyMachPort, 0);
if (gNotifyMachPortRLS) {
CFRunLoopAddSource(CFRunLoopGetCurrent(), gNotifyMachPortRLS, kCFRunLoopDefaultMode);
CFRelease(gNotifyMachPortRLS);
}
CFRelease(gNotifyMachPort);
}
}
static void lwShutdownCallback(
CFMachPortRef port,
void *msg,
CFIndex size,
void *info)
{
mach_msg_header_t *header = (mach_msg_header_t *)msg;
CFNumberRef n = NULL;
static bool amidst_shutdown = false;
static int consoleShutdownState = kIOPMStateConsoleShutdownNone;
static int lastConsoleShutdownState = 0;
if (header->msgh_id == gCPUPowerNotificationToken)
{
// System CPU power status has changed
SystemLoadCPUPowerHasChanged(NULL);
} else if (header->msgh_id == lwNotify.su)
{
// Loginwindow is logging out to begin a several-minute
// software update. We'll suppress the immediately next shoutdown
// and logout messages.
consoleShutdownState = kIOPMStateConsoleSULogoutInitiated;
} else if (header->msgh_id == lwNotify.shutdown)
{
// Loginwindow put a shutdown confirm panel up on screen
// The user has not necessarily even clicked on it yet
amidst_shutdown = true;
consoleShutdownState = kIOPMStateConsoleShutdownPossible;
} else if (header->msgh_id == lwNotify.restart)
{
// Loginwindow put a restart confirm panel up on screen
// The user has not necessarily even clicked on it yet
amidst_shutdown = true;
consoleShutdownState = kIOPMStateConsoleShutdownPossible;
} else if (header->msgh_id == lwNotify.cancel)
{
amidst_shutdown = false;
consoleShutdownState = kIOPMStateConsoleShutdownNone;
} else if (amidst_shutdown
&& (header->msgh_id == lwNotify.pointofnoreturn))
{
// Whatever shutdown or restart that was in progress has succeeded.
// All apps are quit, there's no more user input required. We will
// hereby disable sleep for the remainder of time spent shutting down
// this machine.
_setRootDomainProperty(CFSTR("System Shutdown"), kCFBooleanTrue);
consoleShutdownState = kIOPMStateConsoleShutdownCertain;
}
// Tell interested kernel drivers where we are in the GUI shutdown.
if (lastConsoleShutdownState != consoleShutdownState) {
n = CFNumberCreate(0, kCFNumberIntType, &consoleShutdownState);
if (n) {
_setRootDomainProperty( CFSTR(kIOPMStateConsoleShutdown), n) ;
CFRelease(n);
}
lastConsoleShutdownState = consoleShutdownState;
}
return;
}
Goal: another way of getting notifications for different power/logoff events.. Not sure if it's even possible. Anyway, step 1 is making _SC_CFMachPortCreateWithPort work or alternative of that function.