在Mac OSX上的接收功率的通知(尤其是关机) [英] Receiving power notifications (especially shutdown) on Mac OSX
问题描述
我在C语言编写面向Mac(豹)的应用程序睡眠,唤醒,关机,重启。它通过运行的launchd
作为登录一个launchagent然后开始监测的通知。在code我使用要做到这一点如下:
I'm writing an application in C for the Mac (Leopard) that needs to do some work on receipt of power notifications, e.g. sleep, wake-up, shutdown, restart. It runs via launchd
as a launchagent on login then begins monitoring for notifications. The code I'm using to do this is as follows:
/* 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
)永远被调用。我从来没有得到重新启动或关机( kIOMessageSystemWillRestart
和 kIOMessageSystemWillPowerOff
)。
However, only the top two for sleep and wake (kIOMessageSystemWillSleep
and kIOMessageSystemHasPoweredOn
) ever get called. I never get any notifcations for restart or shutdown (kIOMessageSystemWillRestart
and kIOMessageSystemWillPowerOff
).
我是不是做错了什么?或者是有这会给我的重启和关机通知另一个API?我想preFER保持它作为一个C程序(如多数民众赞成我熟悉的),但我开到(我看了一下登录/注销挂钩,但这些似乎是替代品的任何明智的建议德$ p $赞成的launchd的pcated)。
Am I doing something wrong? Or is there another API that would give me the restart and shutdown notifications? I'd prefer to keep it as a C program (as thats what I'm familiar with) but am open to any sensible suggestions of alternatives (I've had a look at login/logout hooks but these seem to be deprecated in favour of launchd).
在此先感谢您的帮助/提示!
Thanks in advance for any help/tips!
推荐答案
我知道你可以从NSWorkspace的NSWorkspaceWillPowerOffNotification通知,这是不是一个C函数,但没有工作。
I know you can register for the NSWorkspaceWillPowerOffNotification notification from NSWorkspace, which is not a C function but does work.
#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:
Then in WorkspaceResponder.m:
- (void) computerWillShutDownNotification:(NSNotification *)notification {
NSLog(@"Received Shutdown Notification");
}
这篇关于在Mac OSX上的接收功率的通知(尤其是关机)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!