8

私はCforthe Mac(Leopard)で、電源通知の受信時にスリープ、ウェイクアップ、シャットダウン、再起動などの作業を行う必要のあるアプリケーションを作成しています。launchdログイン時にlaunchagentとして実行され、通知の監視を開始します。これを行うために使用しているコードは次のとおりです。

/* 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)
{
    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プログラムとして保持したいと思います(それは私が精通しているものです)が、代替案の賢明な提案を受け入れます(私はログイン/ログアウトフックを見てきましたが、これらは非推奨になっているようです起動の)。

ヘルプ/ヒントを事前に感謝します!

4

2 に答える 2

6

NSWorkspaceからのNSWorkspaceWillPowerOffNotification通知に登録できることを知っています。これは、C関数ではありませんが、機能します。

#import <AppKit/AppKit.h>
#import "WorkspaceResponder.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSNotificationCenter *nc = [[NSWorkspace sharedWorkspace] notificationCenter];
    WorkspaceResponder *mainController = [[WorkspaceResponder alloc] init];

    //register for shutdown notications
    [nc addObserver:mainController
selector:@selector(computerWillShutDownNotification:)
          name:NSWorkspaceWillPowerOffNotification object:nil];
    [[NSRunLoop currentRunLoop] run];
    [pool release];
    return 0;
}

次に、WorkspaceResponder.mで:

- (void) computerWillShutDownNotification:(NSNotification *)notification {
    NSLog(@"Received Shutdown Notification");
}
于 2009-08-26T08:38:17.110 に答える