From e94d5a75360b1541ea709460cdf8b950a97a3ac5 Mon Sep 17 00:00:00 2001 From: mikigal Date: Thu, 30 May 2024 17:42:04 +0200 Subject: [PATCH] Disallow sleep while application is running --- main.go | 1 + sleep.go | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 sleep.go diff --git a/main.go b/main.go index 518dfd8..cb76949 100644 --- a/main.go +++ b/main.go @@ -98,6 +98,7 @@ var ( func main() { checkDebugParam() loaded := loadSettings() + preventSleep() window := g.NewMasterWindow("Anime4K-GUI", 1600, 950, g.MasterWindowFlagsNotResizable) searchHardwareAcceleration() diff --git a/sleep.go b/sleep.go new file mode 100644 index 0000000..dcf4731 --- /dev/null +++ b/sleep.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "syscall" +) + +var ( + kernel32 = syscall.NewLazyDLL("kernel32.dll") + procSetThreadExecutionState = kernel32.NewProc("SetThreadExecutionState") +) + +const ( + ES_CONTINUOUS = 0x80000000 + ES_SYSTEM_REQUIRED = 0x00000001 + ES_DISPLAY_REQUIRED = 0x00000002 +) + +func preventSleep() { + ret, _, err := procSetThreadExecutionState.Call( + uintptr(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED), + ) + + if ret == 0 { + logMessage(fmt.Sprintf("Failed to block sleep/hibernation: %v\n", err), false) + } +}