内容简介:使用windows自带的当系统空闲一段时间后进入睡眠模式不太可靠,用golang写了个小工具定时使用getLastInputInfo获取上次鼠标和键盘的活动时间,超过一段时间未操作进入睡眠状态,当在全屏状态时不进入睡眠状态,如在播放视频时,全屏排除锁屏界面和win+d快捷键进入的桌面。获取是否有程序在全屏使用的golang cgo实现,windows下编译运行需安装gcc,可安装TDM-GCC。如进入睡眠时为休眠,可关闭休眠:
使用windows自带的当系统空闲一段时间后进入睡眠模式不太可靠,用golang写了个小 工具 定时使用getLastInputInfo获取上次鼠标和键盘的活动时间,超过一段时间未操作进入睡眠状态,当在全屏状态时不进入睡眠状态,如在播放视频时,全屏排除锁屏界面和win+d快捷键进入的桌面。
获取是否有程序在全屏使用的golang cgo实现,windows下编译运行需安装gcc,可安装TDM-GCC。
package main /* #include <windows.h> #include <stdio.h> int CheckFullscreen() { int bFullScreen = 0; HWND hWnd = GetForegroundWindow(); RECT rcApp, rcDesk; GetWindowRect(GetDesktopWindow(), &rcDesk); if (hWnd!=GetDesktopWindow() && hWnd!=GetShellWindow()) { GetWindowRect(hWnd, &rcApp); if (rcApp.left<=rcDesk.left && rcApp.top<=rcDesk.top && rcApp.right>=rcDesk.right && rcApp.bottom>=rcDesk.bottom) { char wnd_name[256]; char wnd_title[256]; GetWindowText(hWnd,wnd_title,sizeof(wnd_title)); GetClassName(hWnd, wnd_name, sizeof(wnd_name)); printf("title: %s\n", wnd_title); printf("name: %s\n", wnd_name); if (strcmp(wnd_name, "Windows.UI.Core.CoreWindow") != 0 && strcmp(wnd_name, "WorkerW") != 0){ bFullScreen =1; } } } return bFullScreen; } */ import "C" import ( "fmt" "os/exec" "strings" "syscall" "time" "unsafe" ) var ( user32 = syscall.MustLoadDLL("user32.dll") kernel32 = syscall.MustLoadDLL("kernel32.dll") getLastInputInfo = user32.MustFindProc("GetLastInputInfo") getTickCount = kernel32.MustFindProc("GetTickCount") lastInputInfo struct { cbSize uint32 dwTime uint32 } ) func IdleTime() uint32 { lastInputInfo.cbSize = uint32(unsafe.Sizeof(lastInputInfo)) currentTickCount, _, _ := getTickCount.Call() r1, _, err := getLastInputInfo.Call(uintptr(unsafe.Pointer(&lastInputInfo))) if r1 == 0 { fmt.Println("error getting last input info: " + err.Error()) return 0 } return (uint32(currentTickCount) - lastInputInfo.dwTime) } func Execute(command string) (bool, string, error) { // splitting head => g++ parts => rest of the command parts := strings.Fields(command) head := parts[0] parts = parts[1:len(parts)] out, err := exec.Command(head, parts...).Output() if err != nil { return false, "", err } return true, string(out), nil } func sleepCommandLineImplementation(cmd string) { if cmd == "" { cmd = "C:\\Windows\\System32\\rundll32.exe powrprof.dll,SetSuspendState 0,1,0" } fmt.Println("Sleep implementation [windows], sleep command is [", cmd, "]") _, _, err := Execute(cmd) if err != nil { fmt.Println("Can't execute command [" + cmd + "] : " + err.Error()) } else { fmt.Println("Command correctly executed") } } func sleepDLLImplementation() { var mod = syscall.NewLazyDLL("Powrprof.dll") var proc = mod.NewProc("SetSuspendState") // DLL API : public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent); // ex. : uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Done Title"))), ret, _, _ := proc.Call(0, uintptr(0), // hibernate uintptr(1), // forceCritical uintptr(0)) // disableWakeEvent fmt.Printf("Command executed, result code [" + fmt.Sprint(ret) + "]") } func main() { t := time.NewTicker(5 * time.Second) for range t.C { idle := IdleTime() fmt.Println("idle time", idle) fmt.Println("full screen", C.CheckFullscreen()) if idle > 15*60*1000 && C.CheckFullscreen() != 1 { //sleepCommandLineImplementation("rundll32.exe powrprof.dll,SetSuspendState 0,1,0") sleepDLLImplementation() } } }
如进入睡眠时为休眠,可关闭休眠:
powercfg -h off
参考:
https://bbs.csdn.net/topics/390838652
https://github.com/SR-G/sleep-on-lan
https://www.cnblogs.com/yeshou/p/5197765.html标签:none
以上所述就是小编给大家介绍的《Windows空闲状态时自动睡眠Golang版》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- MySleepart打造智能助眠系统,实现自动睡眠干预和睡眠疾病初筛
- 程序员该如何提高睡眠质量?
- 你睡着了吗?不如起来给你的睡眠分个类吧!
- sql-server – 如何释放空闲SQL Server数据库使用的内存?
- 为了不让GPU等CPU,谷歌提出“数据回波”榨干GPU空闲时间,训练速度提升3倍多
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。