From bc89cc20fc653b6117baf373f09f575e84e09d66 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Thu, 18 Jul 2024 18:25:14 -0700 Subject: [PATCH 1/2] prevent sleep on Windows when playing --- backend/app.go | 10 ++++++++++ backend/nosleep_unsupported.go | 7 +++++++ backend/nosleep_windows.go | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 backend/nosleep_unsupported.go create mode 100644 backend/nosleep_windows.go diff --git a/backend/app.go b/backend/app.go index 2390eaf..50268d7 100644 --- a/backend/app.go +++ b/backend/app.go @@ -127,6 +127,16 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string) _, _ = a.ImageManager.GetCoverThumbnail(coverID) }) + a.PlaybackManager.OnPlaying(func() { + SetSystemSleepDisabled(true) + }) + a.PlaybackManager.OnPaused(func() { + SetSystemSleepDisabled(false) + }) + a.PlaybackManager.OnStopped(func() { + SetSystemSleepDisabled(false) + }) + // Start IPC server if another not already running in a different instance if cli == nil { ipc.DestroyConn() // cleanup socket possibly orphaned by crashed process diff --git a/backend/nosleep_unsupported.go b/backend/nosleep_unsupported.go new file mode 100644 index 0000000..b082169 --- /dev/null +++ b/backend/nosleep_unsupported.go @@ -0,0 +1,7 @@ +//go:build !windows + +package backend + +func SetSystemSleepDisabled(disable bool) { + // no-op +} diff --git a/backend/nosleep_windows.go b/backend/nosleep_windows.go new file mode 100644 index 0000000..6a10d2d --- /dev/null +++ b/backend/nosleep_windows.go @@ -0,0 +1,22 @@ +//go:build windows + +package backend + +import "syscall" + +const ( + ES_CONTINUOUS uint = 0x80000000 + ES_SYSTEM_REQUIRED uint = 0x00000001 +) + +func SetSystemSleepDisabled(disable bool) { + kernel32 := syscall.NewLazyDLL("kernel32.dll") + executionState := kernel32.NewProc("SetThreadExecutionState") + + uType := ES_CONTINUOUS + if disable { + uType |= ES_SYSTEM_REQUIRED + } + + syscall.SyscallN(executionState.Addr(), 1, uintptr(uType), 0, 0) +} From 80e53c2971f512519f458ff53d3b9ab25b47f83d Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Fri, 19 Jul 2024 07:29:57 -0700 Subject: [PATCH 2/2] load DLL only once --- backend/nosleep_windows.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/backend/nosleep_windows.go b/backend/nosleep_windows.go index 6a10d2d..d605e5a 100644 --- a/backend/nosleep_windows.go +++ b/backend/nosleep_windows.go @@ -2,16 +2,30 @@ package backend -import "syscall" +import ( + "sync/atomic" + "syscall" +) const ( ES_CONTINUOUS uint = 0x80000000 ES_SYSTEM_REQUIRED uint = 0x00000001 ) +var ( + sleepDisabled atomic.Bool + executionState *syscall.LazyProc +) + func SetSystemSleepDisabled(disable bool) { - kernel32 := syscall.NewLazyDLL("kernel32.dll") - executionState := kernel32.NewProc("SetThreadExecutionState") + if old := sleepDisabled.Swap(disable); old == disable { + return + } + + if executionState == nil { + kernel32 := syscall.NewLazyDLL("kernel32.dll") + executionState = kernel32.NewProc("SetThreadExecutionState") + } uType := ES_CONTINUOUS if disable {