-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42fc2e6
commit ef7d91d
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package winpowrprof | ||
|
||
const ( | ||
EVENT_SUSPEND = iota | ||
EVENT_RESUME | ||
EVENT_RESUME_AUTOMATIC // Because the user is not present, most applications should do nothing. | ||
) | ||
|
||
type EventListener interface { | ||
Start() error | ||
Close() error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//go:build !windows | ||
|
||
package winpowrprof | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
func NewEventListener(callback func(event int)) (EventListener, error) { | ||
return nil, os.ErrInvalid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package winpowrprof | ||
|
||
// modify from https://github.com/golang/go/blob/b634f6fdcbebee23b7da709a243f3db217b64776/src/runtime/os_windows.go#L257 | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
var ( | ||
modpowerprof = windows.NewLazySystemDLL("powrprof.dll") | ||
procPowerRegisterSuspendResumeNotification = modpowerprof.NewProc("PowerRegisterSuspendResumeNotification") | ||
procPowerUnregisterSuspendResumeNotification = modpowerprof.NewProc("PowerUnregisterSuspendResumeNotification") | ||
) | ||
|
||
const ( | ||
PBT_APMSUSPEND uint32 = 4 | ||
PBT_APMRESUMESUSPEND uint32 = 7 | ||
PBT_APMRESUMEAUTOMATIC uint32 = 18 | ||
) | ||
|
||
const ( | ||
_DEVICE_NOTIFY_CALLBACK = 2 | ||
) | ||
|
||
type _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS struct { | ||
callback uintptr | ||
context uintptr | ||
} | ||
|
||
type eventListener struct { | ||
params _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS | ||
handle uintptr | ||
} | ||
|
||
func NewEventListener(callback func(event int)) (EventListener, error) { | ||
if err := procPowerRegisterSuspendResumeNotification.Find(); err != nil { | ||
return nil, err // Running on Windows 7, where we don't need it anyway. | ||
} | ||
if err := procPowerUnregisterSuspendResumeNotification.Find(); err != nil { | ||
return nil, err // Running on Windows 7, where we don't need it anyway. | ||
} | ||
|
||
var fn interface{} = func(context uintptr, changeType uint32, setting uintptr) uintptr { | ||
switch changeType { | ||
case PBT_APMSUSPEND: | ||
callback(EVENT_SUSPEND) | ||
case PBT_APMRESUMESUSPEND: | ||
callback(EVENT_RESUME) | ||
case PBT_APMRESUMEAUTOMATIC: | ||
callback(EVENT_RESUME_AUTOMATIC) | ||
} | ||
return 0 | ||
} | ||
return &eventListener{ | ||
params: _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS{ | ||
callback: windows.NewCallback(fn), | ||
}, | ||
}, nil | ||
} | ||
|
||
func (l *eventListener) Start() error { | ||
_, _, err := procPowerRegisterSuspendResumeNotification.Call( | ||
_DEVICE_NOTIFY_CALLBACK, | ||
uintptr(unsafe.Pointer(&l.params)), | ||
uintptr(unsafe.Pointer(&l.handle)), | ||
) | ||
return err | ||
} | ||
|
||
func (l *eventListener) Close() error { | ||
_, _, err := procPowerUnregisterSuspendResumeNotification.Call(uintptr(unsafe.Pointer(&l.handle))) | ||
return err | ||
} |