Tag: shutdown

在Mac OSX上接收电源通知(尤其是关机)

我正在用C编写一个用于Mac(Leopard)的应用程序,它需要在接收电源通知时做一些工作,例如睡眠,唤醒,关机,重启。 它通过launchd作为登录时的launchd运行,然后开始监视通知。 我用来做这个的代码如下: /* ask for power notifications */ static void StartPowerNotification(void) { static io_connect_t rootPort; IONotificationPortRef notificationPort; io_object_t notifier; rootPort = IORegisterForSystemPower(&rootPort, &notificationPort, PowerCallback, &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) { […]