|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +// Package power provides system power-state notifications. |
| 4 | +// On Windows it listens for WM_POWERBROADCAST / PBT_APMRESUMESUSPEND |
| 5 | +// so the application can react immediately when the PC wakes from |
| 6 | +// sleep or hibernation. |
| 7 | +package power |
| 8 | + |
| 9 | +import ( |
| 10 | + "log" |
| 11 | + "syscall" |
| 12 | + "unsafe" |
| 13 | + |
| 14 | + "golang.org/x/sys/windows" |
| 15 | +) |
| 16 | + |
| 17 | +// Win32 constants for power broadcast messages. |
| 18 | +const ( |
| 19 | + wmPowerBroadcast = 0x0218 |
| 20 | + pbtAPMResumeSuspend = 0x0007 |
| 21 | + pbtAPMResumeAuto = 0x0012 |
| 22 | +) |
| 23 | + |
| 24 | +// Window class/procedure related Win32 calls. |
| 25 | +var ( |
| 26 | + modUser32 = windows.NewLazySystemDLL("user32.dll") |
| 27 | + procRegisterClassW = modUser32.NewProc("RegisterClassExW") |
| 28 | + procCreateWindowExW = modUser32.NewProc("CreateWindowExW") |
| 29 | + procGetMessageW = modUser32.NewProc("GetMessageW") |
| 30 | + procDefWindowProcW = modUser32.NewProc("DefWindowProcW") |
| 31 | +) |
| 32 | + |
| 33 | +// WNDCLASSEXW mirrors the Win32 WNDCLASSEX structure. |
| 34 | +type wndClassExW struct { |
| 35 | + CbSize uint32 |
| 36 | + Style uint32 |
| 37 | + LpfnWndProc uintptr |
| 38 | + CbClsExtra int32 |
| 39 | + CbWndExtra int32 |
| 40 | + HInstance windows.Handle |
| 41 | + HIcon windows.Handle |
| 42 | + HCursor windows.Handle |
| 43 | + HbrBackground windows.Handle |
| 44 | + LpszMenuName *uint16 |
| 45 | + LpszClassName *uint16 |
| 46 | + HIconSm windows.Handle |
| 47 | +} |
| 48 | + |
| 49 | +// msg mirrors the Win32 MSG structure. |
| 50 | +type msg struct { |
| 51 | + Hwnd uintptr |
| 52 | + Message uint32 |
| 53 | + WParam uintptr |
| 54 | + LParam uintptr |
| 55 | + Time uint32 |
| 56 | + Pt struct{ X, Y int32 } |
| 57 | +} |
| 58 | + |
| 59 | +// resumeCh is the package-level channel shared with the wndproc callback. |
| 60 | +var resumeCh chan struct{} |
| 61 | + |
| 62 | +// ResumeNotifier creates a hidden window that listens for power broadcast |
| 63 | +// messages and returns a channel that receives a value each time the system |
| 64 | +// resumes from sleep or hibernation. |
| 65 | +// |
| 66 | +// The returned channel is buffered (size 1) so a resume event is never lost |
| 67 | +// even if the consumer is briefly busy. Call this once at application startup. |
| 68 | +func ResumeNotifier() <-chan struct{} { |
| 69 | + resumeCh = make(chan struct{}, 1) |
| 70 | + go listenPowerEvents() |
| 71 | + return resumeCh |
| 72 | +} |
| 73 | + |
| 74 | +// listenPowerEvents registers a hidden message-only window and pumps messages. |
| 75 | +func listenPowerEvents() { |
| 76 | + className, _ := syscall.UTF16PtrFromString("WeatherWidgetPowerWnd") |
| 77 | + |
| 78 | + wc := wndClassExW{ |
| 79 | + LpfnWndProc: syscall.NewCallback(powerWndProc), |
| 80 | + LpszClassName: className, |
| 81 | + } |
| 82 | + wc.CbSize = uint32(unsafe.Sizeof(wc)) |
| 83 | + |
| 84 | + ret, _, err := procRegisterClassW.Call(uintptr(unsafe.Pointer(&wc))) |
| 85 | + if ret == 0 { |
| 86 | + log.Printf("power: RegisterClassExW failed: %v", err) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + // HWND_MESSAGE (-3) creates a message-only window (no visible UI). |
| 91 | + hwndMessage := uintptr(^uintptr(2)) // -3 as uintptr |
| 92 | + hwnd, _, err := procCreateWindowExW.Call( |
| 93 | + 0, |
| 94 | + uintptr(unsafe.Pointer(className)), |
| 95 | + 0, |
| 96 | + 0, 0, 0, 0, 0, |
| 97 | + hwndMessage, 0, 0, 0, |
| 98 | + ) |
| 99 | + if hwnd == 0 { |
| 100 | + log.Printf("power: CreateWindowExW failed: %v", err) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + log.Printf("power: listening for resume events (hwnd=%#x)", hwnd) |
| 105 | + |
| 106 | + // Message pump — blocks forever (runs in its own goroutine). |
| 107 | + var m msg |
| 108 | + for { |
| 109 | + ret, _, _ := procGetMessageW.Call( |
| 110 | + uintptr(unsafe.Pointer(&m)), |
| 111 | + hwnd, 0, 0, |
| 112 | + ) |
| 113 | + if ret == 0 || ret == ^uintptr(0) { |
| 114 | + // WM_QUIT or error |
| 115 | + return |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// powerWndProc handles window messages for the hidden power-listener window. |
| 121 | +func powerWndProc(hwnd uintptr, umsg uint32, wparam, lparam uintptr) uintptr { |
| 122 | + if umsg == wmPowerBroadcast { |
| 123 | + switch wparam { |
| 124 | + case pbtAPMResumeSuspend, pbtAPMResumeAuto: |
| 125 | + log.Printf("power: system resumed from sleep/hibernate (event=%#x)", wparam) |
| 126 | + // Non-blocking send — drop if channel already has a pending signal. |
| 127 | + select { |
| 128 | + case resumeCh <- struct{}{}: |
| 129 | + default: |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + ret, _, _ := procDefWindowProcW.Call(hwnd, uintptr(umsg), wparam, lparam) |
| 135 | + return ret |
| 136 | +} |
0 commit comments