Note that there are some explanatory texts on larger screens.

plurals
  1. POReceiving power notifications (especially shutdown) on Mac OSX
    text
    copied!<p>I'm writing an application in C for the Mac (Leopard) that needs to do some work on receipt of power notifications, e.g. sleep, wake-up, shutdown, restart. It runs via <code>launchd</code> as a launchagent on login then begins monitoring for notifications. The code I'm using to do this is as follows:</p> <pre><code>/* ask for power notifications */ static void StartPowerNotification(void) { static io_connect_t rootPort; IONotificationPortRef notificationPort; io_object_t notifier; rootPort = IORegisterForSystemPower(&amp;rootPort, &amp;notificationPort, PowerCallback, &amp;notifier); if (!rootPort) exit (1); CFRunLoopAddSource (CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notificationPort), kCFRunLoopDefaultMode); } /* perform actions on receipt of power notifications */ void PowerCallback (void *rootPort, io_service_t y, natural_t msgType, void *msgArgument) { switch (msgType) { case kIOMessageSystemWillSleep: /* perform sleep actions */ break; case kIOMessageSystemHasPoweredOn: /* perform wakeup actions */ break; case kIOMessageSystemWillRestart: /* perform restart actions */ break; case kIOMessageSystemWillPowerOff: /* perform shutdown actions */ break; } } </code></pre> <p>However, only the top two for sleep and wake (<code>kIOMessageSystemWillSleep</code> and <code>kIOMessageSystemHasPoweredOn</code>) <strong>ever</strong> get called. I never get any notifcations for restart or shutdown (<code>kIOMessageSystemWillRestart</code> and <code>kIOMessageSystemWillPowerOff</code>).</p> <p>Am I doing something wrong? Or is there another API that would give me the restart and shutdown notifications? I'd prefer to keep it as a C program (as thats what I'm familiar with) but am open to any sensible suggestions of alternatives (I've had a look at login/logout hooks but these seem to be deprecated in favour of launchd).</p> <p>Thanks in advance for any help/tips!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload