use
5.008008;
our
$VERSION
=
'0.02'
;
XSLoader::load(
'Mac::SleepEvent'
,
$VERSION
);
PerlObjCBridge::preloadSelectors(
'SleepEvent'
);
sub
new {
my
(
$class
,
%args
) =
@_
;
my
$self
= {
sleep_callback
=>
$args
{
'sleep'
} ||
sub
{},
wake_callback
=>
$args
{
'wake'
} ||
sub
{},
logout_callback
=>
$args
{
'logout'
} ||
sub
{},
};
_setup_appkit();
return
bless
$self
,
$class
;
}
sub
_setup_appkit {
my
$path
= NSString->stringWithCString_(
'/System/Library/Frameworks/AppKit.framework'
);
my
$appkit
= NSBundle->alloc->init->initWithPath_(
$path
);
$appkit
->load
if
$appkit
;
if
(
$appkit
->isLoaded) {
no
strict
'refs'
;
for
my
$class
(
qw(NSWorkspace)
) {
@{
$class
.
'::ISA'
} =
'PerlObjCBridge'
;
}
}
else
{
die
"unable to load AppKit framework"
;
}
}
sub
listen
{
my
$self
=
shift
;
NSWorkspace->sharedWorkspace->notificationCenter->addObserver_selector_name_object_(
$self
,
'asleep'
,
'NSWorkspaceWillSleepNotification'
,
undef
);
NSWorkspace->sharedWorkspace->notificationCenter->addObserver_selector_name_object_(
$self
,
'awake'
,
'NSWorkspaceDidWakeNotification'
,
undef
);
NSWorkspace->sharedWorkspace->notificationCenter->addObserver_selector_name_object_(
$self
,
'logout'
,
'NSWorkspaceWillPowerOffNotification'
,
undef
);
NSRunLoop->currentRunLoop->run;
}
sub
asleep {
my
$self
=
shift
;
$self
->{sleep_callback}();
}
sub
awake {
my
$self
=
shift
;
$self
->{wake_callback}();
}
sub
logout {
my
$self
=
shift
;
$self
->{logout_callback}();
}
1;