2009-08-24 8 views
5

Mac 용 C (Leopard)의 전원 알림 수신과 관련하여 작업해야하는 응용 프로그램을 작성 중입니다. sleep, wake-up, shutdown, restart. 로그인시 launchagent로 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, &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; 
    } 
} 

그러나, 휴면 (kIOMessageSystemWillSleepkIOMessageSystemHasPoweredOn)의 최고 두 이제까지가 호출되는 다음과 같이 내가이 일을 사용하고 코드입니다. 재시작 또는 종료 (kIOMessageSystemWillRestartkIOMessageSystemWillPowerOff)에 대한 알림을받지 못했습니다.

내가 잘못 했나요? 또는 다시 시작 및 종료 알림을 제공하는 또 다른 API가 있습니까? 나는 C 프로그램으로 유지하고 싶다. (내가 익숙한 것으로서) 그러나 대안의 현명한 제안에 개방되어있다. (로그인/로그 아웃 훅을 살펴 보았다.하지만 이것들은 더 이상 사용되지 않을 것으로 보인다. 발사의).

미리 도움 주시면 감사하겠습니다.

답변

4

C 작업이 아니지만 작동하는 NSWorkspace의 NSWorkspaceWillPowerOffNotification 알림에 등록 할 수 있다는 것을 알고 있습니다. WorkspaceResponder.m에서 다음

#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; 
} 

:

IORegisterForSystemPower 당신이 찾는 이벤트를 제공하지 않습니다 사용
- (void) computerWillShutDownNotification:(NSNotification *)notification { 
    NSLog(@"Received Shutdown Notification"); 
} 
+0

감사합니다. 이러한 알림을 수신하기 위해 창이나 도킹 아이콘이 필요한지 알고 있습니까? – binarybob

+1

아니요, 그렇지 않습니다. 몇 가지 샘플 코드를 추가했습니다. –

+0

다시 한번 감사드립니다. Rob, 많이 고맙습니다. – binarybob

0

. 함수 문서에서 인용 :

/*! @function IORegisterForSystemPower는

@abstract는 시스템에 잠 & 웨이크 알림을 수신 할 목적으로 루트 전원 도메인 IOService 에 발신자를 연결합니다.

시스템 종료 및 다시 시작 알림을 제공하지 않습니다.

관련 문제