Windows空闲状态时自动睡眠Golang版

栏目: Go · 发布时间: 5年前

内容简介:使用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

标签:none


以上所述就是小编给大家介绍的《Windows空闲状态时自动睡眠Golang版》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

从0开始做运营 入门篇

从0开始做运营 入门篇

张亮 / 4.99元

此书是《从零开始做运营》系列的入门篇。 在互联网产品经理热的今天,关于传统的网站与产品运营的书籍一直非常缺乏,很多有志于互联网行业的年轻人并不明白一款产品、一个网站的策划、上线、成长、成熟直到衰落的过程中,除了产品和网站本身的设计之外,还有一块非常重要的工作是针对网站与产品生命周期的持续运营。 网站与产品运营是一个非常辛苦而非常有趣的事情,希望本书可以为有志于从事互联网网站与产品运营的......一起来看看 《从0开始做运营 入门篇》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

SHA 加密
SHA 加密

SHA 加密工具