2009-08-24 12 views
5

पर बिजली अधिसूचनाएं (विशेष रूप से शटडाउन) प्राप्त करना मैं मैक (तेंदुए) के लिए सी में एक आवेदन लिख रहा हूं जिसे बिजली अधिसूचनाओं की प्राप्ति पर कुछ काम करने की आवश्यकता है, उदाहरण के लिए सो जाओ, जाग जाओ, बंद करें, पुनरारंभ करें। यह लॉगिन पर लॉन्चेंट के रूप में 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) 
{ 
    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) के लिए शीर्ष दो कभी कहा जाता हो: कोड मैं यह करने के उपयोग कर रहा हूँ इस प्रकार है। मुझे पुनरारंभ या शट डाउन (kIOMessageSystemWillRestart और kIOMessageSystemWillPowerOff) के लिए कभी भी कोई नोटिफिकेशन नहीं मिलता है।

क्या मैं कुछ गलत कर रहा हूं? या क्या कोई और एपीआई है जो मुझे पुनरारंभ और शट डाउन अधिसूचनाएं देगी? मैं इसे एक सी प्रोग्राम के रूप में रखना पसंद करूंगा (जैसा कि मैं परिचित हूं) लेकिन विकल्पों के किसी भी समझदार सुझाव के लिए खुला हूं (मैंने लॉगिन/लॉगआउट हुक पर एक नज़र डाली है लेकिन इन्हें पक्ष में बहिष्कृत किया गया है लॉन्च के)।

किसी भी मदद/सुझाव के लिए अग्रिम धन्यवाद!

उत्तर

4

मुझे पता है कि आप NSWorkspace से NSWorkspaceWillPowerOffNotification अधिसूचना के लिए पंजीकरण कर सकते हैं, जो कि सी फ़ंक्शन नहीं है बल्कि काम करता है।

#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"); 
} 
+0

इसके लिए धन्यवाद! क्या आपको पता है कि इन सूचनाओं को प्राप्त करने के लिए मुझे विंडो या डॉक आइकन की आवश्यकता है या नहीं? – binarybob

+1

नहीं, आप नहीं करते हैं। मैंने कुछ नमूना कोड जोड़ा। –

+0

फिर से धन्यवाद रॉब, यह बहुत सराहना की है। – binarybob

0

का उपयोग IORegisterForSystemPower घटनाओं आप चाहते हैं प्रदान नहीं करता है। फ़ंक्शन प्रलेखन से उद्धरण:

/*! @function IORegisterForSystemPower

@abstract प्रणाली के लिए नींद & मद्देनजर सूचनाएं प्राप्त करने के प्रयोजन के लिए रूट पावर डोमेन IOService को फोन करने वाले से जोड़ता है।

सिस्टम शटडाउन प्रदान नहीं करता है और अधिसूचनाओं को पुनरारंभ नहीं करता है।