diff --git a/win/README.txt b/win/README.txt index c66ae82..298bf97 100644 --- a/win/README.txt +++ b/win/README.txt @@ -4,8 +4,9 @@ SYNOPSIS csminer from https://cryptonote.social is an easy-to-use CPU miner for Monero intended to provide "set it and forget it" mining for your existing laptop and desktop machines. By default, csminer -mines with a single thread, and only when the screen is locked or the screensaver is running. It -can be configured to always mine or mine with more threads using the options described below. +mines with a single thread, and only when the screen is locked or the screensaver is running. On a +laptop, csminer will mine only when on AC power to avoid draining the battery. It can be +configured to always mine or mine with more threads using the options described below. USAGE diff --git a/win/csminer.go b/win/csminer.go index 3107661..9c61170 100644 --- a/win/csminer.go +++ b/win/csminer.go @@ -31,6 +31,7 @@ func (ss *WinScreenStater) GetScreenStateChannel() (chan csminer.ScreenState, er // TODO: Also monitor for ac vs battery power state currentlyLocked := false isIdle := false + batteryPower := false for { select { case m := <-chanMessages: @@ -56,6 +57,22 @@ func (ss *WinScreenStater) GetScreenStateChannel() (chan csminer.ScreenState, er } close(m.ChanOk) case <-time.After(10 * time.Second): + b, err := isBatteryPower() + if err != nil { + crylog.Error("failed to get battery power state:", err) + } else { + if b != batteryPower { + if b { + crylog.Info("Detected battery power") + batteryPower = true + ret <- csminer.ScreenState(csminer.BATTERY_POWER) + } else { + crylog.Info("Detected AC power") + batteryPower = false + ret <- csminer.ScreenState(csminer.AC_POWER) + } + } + } if currentlyLocked { continue } @@ -123,13 +140,15 @@ type systemPowerStatus struct { } func isBatteryPower() (bool, error) { - getSystemPowerStatus := libuser32.NewProc("SystemParametersInfoW") + getSystemPowerStatus := libkernel32.NewProc("GetSystemPowerStatus") var s systemPowerStatus res, _, err := syscall.Syscall(getSystemPowerStatus.Addr(), 1, uintptr(unsafe.Pointer(&s)), 0, 0) if res == 0 { return false, err } - crylog.Info("systemPowerStatus: ", s) + if s.aclineStatus == 0 { + return true, nil + } return false, nil } diff --git a/win/make.bat b/win/make.bat index 2d0f4c2..6e27199 100644 --- a/win/make.bat +++ b/win/make.bat @@ -1,11 +1,9 @@ del csminer.exe del csminer_res.o -del ..\rx\cpp\rxlib.cpp.o -del ..\rx\cpp\rxlib.o -cd ..\rx\cpp -call make.bat -cd ..\..\win windres csminer.rc csminer_res.o -ld -relocatable csminer_res.o ..\rx\cpp\rxlib.cpp.o -o ..\rx\cpp\rxlib.o -move ..\rx\cpp\rxlib.o ..\rx\cpp\rxlib.cpp.o -go build -x -ldflags="-s -w" csminer.go +cd ..\..\rxlib +call make.bat +cd ..\csminer\win +ld -relocatable csminer_res.o ..\..\rxlib\rxlib.cpp.o -o ..\..\rxlib\rxlib.o +move ..\..\rxlib\rxlib.o ..\..\rxlib\rxlib.cpp.o +go build -a -ldflags="-s -w" csminer.go