PDA

View Full Version : How to get Restart,Shutdown .... events




Sunil
Jul 13, 2007, 07:55 AM
Hi Guys,

I have written a network application in cocoa.
In my application I want to capture the Logout,Restart,Shutdown events.
Based on these events I want to send some message to my server.


After reading the documentation on internet regarding system events I came to know that whenever user choose "Shutdown" or "Restart" from the Apple Menu the running application gets "KAEQuitApplication" event but not any specific event whether the application is quiting by "Shutdown" or "Restart" or "Log Out". The "KAEQuitApplication" event also get by the user when he/she quit their application by clicking at the top left cross button of the application window.

I m looking to get the specific event related with each action taken by the user i.e. "Shutdown" or "Restart" or "Log Out".
Is it possible to get the different event for each of the action taken by the user i.e. "Shutdown" or "Restart" or "Log Out"?.

waiting for the reply.

Thanks,
sunil



whooleytoo
Jul 13, 2007, 11:56 AM
I've no idea. But you could try seeing if a notification is sent, by registering for all distributed notifications; restarting your Mac, and seeing what notifications were received.

Catfish_Man
Jul 13, 2007, 03:18 PM
I've no idea. But you could try seeing if a notification is sent, by registering for all distributed notifications; restarting your Mac, and seeing what notifications were received.

http://www.tildesoft.com/Programs.html <-- NotificationWatcher could work as well.

kainjow
Jul 13, 2007, 03:37 PM
I think these may be what you're looking for: kIOMessageSystemWillPowerOff, kIOMessageSystemWillSleep, kIOMessageSystemWillRestart

semaja2
Jul 13, 2007, 08:44 PM
Heres some code from my Utility Lock, i believe you could just change the constants in the case;

PowerManagment.m
#import "PowerManagment.h"

void powerCallback(void *refCon, io_service_t service, natural_t messageType, void *messageArgument)
{
[(PowerManagment *)refCon powerMessageReceived: messageType withArgument: messageArgument];
}

@implementation PowerManagment
- (void)powerMessageReceived:(natural_t)messageType withArgument:(void *) messageArgument
{
switch (messageType)
{
case kIOMessageSystemWillSleep:{
IOAllowPowerChange(root_port, (long)messageArgument);
NSLog(@"System is going to sleep");
break;
}
case kIOMessageCanSystemSleep:
// I doné─˘t know if this will ever be askedé─Â
IOAllowPowerChange(root_port, (long)messageArgument);
break;

case kIOMessageSystemHasPoweredOn:{
NSLog(@"System has awaken from sleep");
break;
}
}
}

- (void)awakeFromNib {
IONotificationPortRef notificationPort;
root_port = IORegisterForSystemPower(self, &notificationPort, powerCallback, &notifier);
NSAssert(root_port != nil, @"IORegisterForSystemPower failed");
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notificationPort), kCFRunLoopDefaultMode);
}

PowerManagment.h
#import <Cocoa/Cocoa.h>
#import <IOKit/pwr_mgt/IOPMLib.h>
#import <IOKit/IOMessage.h>

@interface PowerManagment : NSObject {
io_connect_t root_port;
io_object_t notifier;
}
- (void)powerMessageReceived:(natural_t)messageType withArgument:(void *) messageArgument;