csminer/linux/csminer.go

66 lines
1.7 KiB
Go
Raw Permalink Normal View History

2020-06-20 01:21:08 +03:00
// Copyright 2020 cryptonote.social. All rights reserved. Use of this source code is governed by
// the license found in the LICENSE file.
package main
// main() for the Linux version of csminer w/ Gnome screen monitoring support
import (
"fmt"
"github.com/cryptonote-social/csminer"
"github.com/cryptonote-social/csminer/crylog"
"github.com/godbus/dbus/v5"
2020-06-20 01:21:08 +03:00
)
func main() {
2020-08-21 06:13:30 +03:00
csminer.MultiMain(GnomeMachineStater{}, "csminer "+csminer.VERSION_STRING+" (linux)")
2020-06-20 01:21:08 +03:00
}
2020-08-21 06:13:30 +03:00
type GnomeMachineStater struct {
2020-06-20 01:21:08 +03:00
}
2020-08-21 06:13:30 +03:00
func (s GnomeMachineStater) GetMachineStateChannel(saver bool) (chan csminer.MachineState, error) {
ret := make(chan csminer.MachineState)
if !saver {
return ret, nil // return channel on which we never send updates
}
2020-06-20 01:21:08 +03:00
bus, err := dbus.ConnectSessionBus()
if err != nil {
crylog.Error("dbus connection failed")
2020-06-20 01:21:08 +03:00
return nil, err
}
err = bus.AddMatchSignal(
// dbus.WithMatchObjectPath("/org/gnome/ScreenSaver"),
dbus.WithMatchInterface("org.gnome.ScreenSaver"),
dbus.WithMatchMember("ActiveChanged"),
)
dChan := make(chan *dbus.Message, 128)
bus.Eavesdrop(dChan)
go func() {
defer bus.Close()
for m := range dChan {
if m == nil {
crylog.Warn("got nil message")
continue
}
if len(m.Body) > 0 {
str := fmt.Sprintf("%v", m.Body[0])
if str == "true" {
crylog.Info("Gnome screensaver turned on")
2020-08-21 06:13:30 +03:00
ret <- csminer.MachineState(csminer.SCREEN_IDLE)
2020-06-20 01:21:08 +03:00
continue
} else if str == "false" {
crylog.Info("Gnome screensaver turned off")
2020-08-21 06:13:30 +03:00
ret <- csminer.MachineState(csminer.SCREEN_ACTIVE)
2020-06-20 01:21:08 +03:00
continue
}
}
2020-07-26 05:53:34 +03:00
//crylog.Info("ignoring dbus message:", m)
2020-06-20 01:21:08 +03:00
}
crylog.Error("dbus listener goroutine exiting")
}()
return ret, nil
}