-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch_switch.go
126 lines (114 loc) · 3.18 KB
/
watch_switch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package mac_switch_watch
//#cgo darwin LDFLAGS: -framework IOKit -framework CoreFoundation
/*
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <mach/mach_port.h>
#include <mach/mach_interface.h>
#include <mach/mach_init.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOMessage.h>
io_connect_t root_port;
io_object_t notifier_obj;
IONotificationPortRef notify_port_ref;
extern void CallbackOnCanDevicePowerOff();
extern void CallbackOnCanSystemPowerOff();
extern void CallbackOnCanSystemSleep();
extern void CallbackOnDeviceHasPoweredOff();
extern void CallbackOnDeviceHasPoweredOn();
extern void CallbackOnDeviceWillNotPowerOff();
extern void CallbackOnDeviceWillPowerOff();
extern void CallbackOnDeviceWillPowerOn();
extern void CallbackOnSystemHasPoweredOn();
extern void CallbackOnSystemPagingOff();
extern void CallbackOnSystemWillNotPowerOff();
extern void CallbackOnSystemWillNotSleep();
extern void CallbackOnSystemWillPowerOff();
extern void CallbackOnSystemWillPowerOn();
extern void CallbackOnSystemWillRestart();
extern void CallbackOnSystemWillSleep();
void sleep_callback( void * ref_con, io_service_t service, natural_t msg_type, void * msg_arg )
{
switch ( msg_type )
{
case kIOMessageCanDevicePowerOff:
CallbackOnCanDevicePowerOff();
break;
case kIOMessageCanSystemPowerOff:
CallbackOnCanSystemPowerOff ();
break;
case kIOMessageCanSystemSleep:
CallbackOnCanSystemSleep ();
break;
case kIOMessageDeviceHasPoweredOff:
CallbackOnDeviceHasPoweredOff ();
break;
case kIOMessageDeviceHasPoweredOn:
CallbackOnDeviceHasPoweredOn ();
break;
case kIOMessageDeviceWillNotPowerOff:
CallbackOnDeviceWillNotPowerOff ();
break;
case kIOMessageDeviceWillPowerOff:
CallbackOnDeviceWillPowerOff ();
break;
case kIOMessageDeviceWillPowerOn:
CallbackOnDeviceWillPowerOn ();
break;
case kIOMessageSystemHasPoweredOn:
CallbackOnSystemHasPoweredOn ();
break;
case kIOMessageSystemPagingOff:
CallbackOnSystemPagingOff ();
break;
case kIOMessageSystemWillNotPowerOff:
CallbackOnSystemWillNotPowerOff ();
break;
case kIOMessageSystemWillNotSleep:
CallbackOnSystemWillNotSleep ();
break;
case kIOMessageSystemWillPowerOff:
CallbackOnSystemWillPowerOff ();
break;
case kIOMessageSystemWillPowerOn:
CallbackOnSystemWillPowerOn ();
break;
case kIOMessageSystemWillRestart:
CallbackOnSystemWillRestart ();
break;
case kIOMessageSystemWillSleep:
CallbackOnSystemWillSleep ();
break;
default:
break;
}
}
int start_watch_sleep()
{
void* ref_con;
root_port = IORegisterForSystemPower( ref_con, ¬ify_port_ref, sleep_callback, ¬ifier_obj );
if ( root_port == 0 )
{
printf("IORegisterForSystemPower failed\n");
return 1;
}
CFRunLoopAddSource( CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(notify_port_ref), kCFRunLoopCommonModes );
CFRunLoopRun();
return (0);
}
void remove_notifier(){
CFRunLoopRemoveSource( CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(notify_port_ref),
kCFRunLoopCommonModes );
IODeregisterForSystemPower( ¬ifier_obj );
IOServiceClose( root_port );
IONotificationPortDestroy( notify_port_ref );
}
*/
import "C"
func Watch() {
C.start_watch_sleep()
defer C.remove_notifier()
}