私はMac用のC(Leopard)のアプリケーションを作成しています。スリープ、起床、シャットダウン、再起動。ログイン時に起動エージェントとしてlaunchd
を実行し、通知の監視を開始します。次のように私はこれを行うために使用しているコードは次のとおりです。ただしMac OSXで電力通知(特にシャットダウン)を受け取る
/* ask for power notifications */
static void StartPowerNotification(void)
{
static io_connect_t rootPort;
IONotificationPortRef notificationPort;
io_object_t notifier;
rootPort = IORegisterForSystemPower(&rootPort, ¬ificationPort,
PowerCallback, ¬ifier);
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;
}
}
、睡眠と覚醒(kIOMessageSystemWillSleep
とkIOMessageSystemHasPoweredOn
)のための唯一の上位2 これまでが呼び出されます。私は再起動やシャットダウン(kIOMessageSystemWillRestart
とkIOMessageSystemWillPowerOff
)の通知を得ることはありません。
何か間違っていますか?または、私に再起動とシャットダウンの通知を与える別のAPIがありますか?私はそれをCプログラム(私がよく知っているもの)として保つことを好むでしょうが、選択肢の賢明な提案に開放しています(私はログイン/ログアウトフックを見てきましたが、これらは廃止されているようですlaunchdの)
ご協力いただきありがとうございます!
ありがとうございました!これらの通知を受け取るためにウィンドウまたはドックアイコンが必要かどうか知っていますか? – binarybob
いいえ、そうではありません。サンプルコードを追加しました。 –
もう一度、Robに感謝します。 – binarybob