make battery state detection work on windows

This commit is contained in:
cryptonote-social 2020-08-20 19:58:19 -07:00
parent 1c2f8c770c
commit 5a42c68d78
3 changed files with 30 additions and 12 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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