Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
plugin: adapt thermal as a plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Rachel Chen <rachel@chens.email>
  • Loading branch information
zllovesuki committed Dec 7, 2020
1 parent 5d04716 commit e1f5409
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 152 deletions.
21 changes: 14 additions & 7 deletions controller/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,20 @@ func New(conf RunConfig) (*Controller, error) {

// TODO: allow user to specify profiles
thermalCfg := thermal.Config{
WMI: wmi,
PowerCfg: powercfg,
Profiles: thermal.GetDefaultThermalProfiles(),
WMI: wmi,
PowerCfg: powercfg,
Profiles: thermal.GetDefaultThermalProfiles(),
AutoThermal: conf.EnabledFeatures.AutoThermalProfile,
AutoThermalConfig: struct {
PluggedIn string
Unplugged string
}{
PluggedIn: "Performance",
Unplugged: "Silent",
},
}

profile, err := thermal.NewControl(thermalCfg)
thermal, err := thermal.NewControl(thermalCfg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,8 +95,7 @@ func New(conf RunConfig) (*Controller, error) {

// order powercfg to last
config.Register(battery)
config.Register(profile)
config.Register(powercfg)
config.Register(thermal)
config.Register(kbCtrl)

control, err := newController(Config{
Expand All @@ -97,9 +104,9 @@ func New(conf RunConfig) (*Controller, error) {
Plugins: []plugin.Plugin{
volCtrl,
kbCtrl,
thermal,
},

Thermal: profile,
Registry: config,

EnabledFeatures: conf.EnabledFeatures,
Expand Down
5 changes: 0 additions & 5 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/zllovesuki/G14Manager/system/persist"
"github.com/zllovesuki/G14Manager/system/plugin"
"github.com/zllovesuki/G14Manager/system/power"
"github.com/zllovesuki/G14Manager/system/thermal"
"github.com/zllovesuki/G14Manager/util"

"github.com/pkg/errors"
Expand Down Expand Up @@ -62,7 +61,6 @@ type Config struct {
WMI atkacpi.WMI

Plugins []plugin.Plugin
Thermal *thermal.Control
Registry persist.ConfigRegistry

EnabledFeatures Features
Expand Down Expand Up @@ -92,9 +90,6 @@ func newController(conf Config) (*Controller, error) {
if conf.WMI == nil {
return nil, errors.New("[controller] nil WMI is invalid")
}
if conf.Thermal == nil {
return nil, errors.New("[controller] nil Thermal is invalid")
}
if conf.Registry == nil {
return nil, errors.New("[controller] nil Registry is invalid")
}
Expand Down
53 changes: 12 additions & 41 deletions controller/controller_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controller
import (
"context"
"encoding/binary"
"fmt"
"log"
"runtime"

Expand Down Expand Up @@ -158,17 +157,7 @@ func (c *Controller) handleWorkQueue(haltCtx context.Context) {

case ev := <-c.workQueueCh[fnThermalProfile].clean:
log.Printf("[controller] Fn + F5 pressed %d times\n", ev.Counter)
next, err := c.Config.Thermal.NextProfile(int(ev.Counter))
message := fmt.Sprintf("Thermal plan changed to %s", next)
if err != nil {
log.Println(err)
message = err.Error()
}
c.notifyQueueCh <- util.Notification{
Title: "Toggle Thermal Plan",
Message: message,
}
c.workQueueCh[fnPersistConfigs].noisy <- struct{}{}
c.notifyPlugins(plugin.EvtSentinelCycleThermalProfile, ev.Counter)

case ev := <-c.workQueueCh[fnCheckCharger].clean:
function := make([]byte, 4)
Expand Down Expand Up @@ -198,29 +187,11 @@ func (c *Controller) handleWorkQueue(haltCtx context.Context) {
}

case ev := <-c.workQueueCh[fnAutoThermal].clean:
if c.Config.EnabledFeatures.AutoThermalProfile {
pluggedInStatus := ev.Data.(chargerStatus)
log.Printf("[controller] automatically switching thermal profile. Charger is: %s\n", pluggedInStatus)

var next string
var err error
var message string
// TODO: make it configurable
if pluggedInStatus == chargerPluggedIn {
next, err = c.Config.Thermal.SwitchToProfile("Performance")
} else {
next, err = c.Config.Thermal.SwitchToProfile("Silent")
}
if err != nil {
log.Println(err)
message = err.Error()
} else {
message = fmt.Sprintf("Thermal plan changed to %s", next)
}
c.notifyQueueCh <- util.Notification{
Title: "Automatic Thermal Plan Switching: " + pluggedInStatus.String(),
Message: message,
}
pluggedInStatus := ev.Data.(chargerStatus)
if pluggedInStatus == chargerPluggedIn {
c.notifyPlugins(plugin.EvtChargerPluggedIn, nil)
} else {
c.notifyPlugins(plugin.EvtChargerUnplugged, nil)
}

case <-c.workQueueCh[fnPersistConfigs].clean:
Expand All @@ -239,10 +210,6 @@ func (c *Controller) handleWorkQueue(haltCtx context.Context) {
c.errorCh <- errors.Wrap(err, "[controller] error applying configurations")
return
}
c.notifyQueueCh <- util.Notification{
Title: "Settings Loaded from Registry",
Message: fmt.Sprintf("Current Thermal Plan: %s", c.Config.Thermal.CurrentProfile().Name),
}

case ev := <-c.workQueueCh[fnHwCtrl].clean:
keyCode := ev.Data.(uint32)
Expand Down Expand Up @@ -286,10 +253,14 @@ func (c *Controller) notifyPlugins(evt plugin.Event, val interface{}) {
func (c *Controller) handlePluginCallback(haltCtx context.Context) {
for {
select {
case evt := <-c.pluginCbCh:
switch evt {
case t := <-c.pluginCbCh:
switch t.Event {
case plugin.CbPersistConfig:
c.workQueueCh[fnPersistConfigs].noisy <- struct{}{}
case plugin.CbNotifyToast:
if n, ok := t.Value.(util.Notification); ok {
c.notifyQueueCh <- n
}
}
case <-haltCtx.Done():
return
Expand Down
8 changes: 6 additions & 2 deletions cxx/plugin/keyboard/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,14 @@ func (c *Control) loop(haltCtx context.Context, cb chan<- plugin.Callback) {
c.errChan <- c.toggleTouchPad()
case keyboard.KeyFnDown:
c.errChan <- c.brightnessDown()
cb <- plugin.CbPersistConfig
cb <- plugin.Callback{
Event: plugin.CbPersistConfig,
}
case keyboard.KeyFnUp:
c.errChan <- c.brightnessUp()
cb <- plugin.CbPersistConfig
cb <- plugin.Callback{
Event: plugin.CbPersistConfig,
}
case keyboard.KeyFnLeft, keyboard.KeyFnRight:
if remap, ok := c.Config.Remap[keycode]; ok {
c.emulateKeyPress(remap)
Expand Down
22 changes: 8 additions & 14 deletions system/plugin/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const (
EvtChargerUnplugged
EvtSentinelInitKeyboard
EvtSentinelKeyboardBrightnessOff
EvtSentinelCycleThermalProfile

CbPersistConfig
CbNotifyToast
)

func (e Event) String() string {
Expand All @@ -23,19 +27,9 @@ func (e Event) String() string {
"Event: Charged unplugged",
"Event (sentinel): Initializa keyboard",
"Event (sentinel): Keyboard backlight off",
}[e]
}
"Event (sentinel): Cycle thermal profile",

// Callback defines the type of notification from plugins to controller
type Callback int

// Define all the possible plugin->controller callbacks
const (
CbPersistConfig Callback = iota
)

func (c Callback) String() string {
return [...]string{
"Callback: Request to save config",
}[c]
"Callback: Request to persist config",
"Callback: Request to notify user",
}[e]
}
5 changes: 5 additions & 0 deletions system/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ type Notification struct {
Value interface{}
}

type Callback struct {
Event Event
Value interface{}
}

// Plugin will receive hardware events from controller
type Plugin interface {
Initialize() error
Expand Down
50 changes: 0 additions & 50 deletions system/power/powercfg.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package power

import (
"bytes"
"encoding/gob"
"errors"
"log"
"os/exec"
"regexp"
"strings"
"syscall"

"github.com/zllovesuki/G14Manager/system/persist"
)

const (
Expand Down Expand Up @@ -103,52 +99,6 @@ func (p *Cfg) Set(planName string) (nextPlan string, err error) {
return
}

var _ persist.Registry = &Cfg{}

// Name satisfies persist.Registry
func (p *Cfg) Name() string {
return powerPersistKey
}

// Value satisfies persist.Registry
func (p *Cfg) Value() []byte {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(p.activePlan); err != nil {
return nil
}
return buf.Bytes()
}

// Load satisfies persist.Registry
func (p *Cfg) Load(v []byte) error {
if len(v) == 0 {
return nil
}
activePlan := plan{}
buf := bytes.NewBuffer(v)
dec := gob.NewDecoder(buf)
if err := dec.Decode(&activePlan); err != nil {
return err
}
p.activePlan = activePlan
return nil
}

// Apply satisfies persist.Registry
func (p *Cfg) Apply() error {
if p.activePlan.Name == "" {
return nil
}
_, err := p.Set(p.activePlan.Name)
return err
}

// Close satisfied persist.Registry
func (p *Cfg) Close() error {
return nil
}

// run will attempt to execute in command line without showing the console window
func run(command string, args ...string) ([]byte, error) {
cmd := exec.Command(command, args...)
Expand Down
28 changes: 0 additions & 28 deletions system/power/powercfg_test.go

This file was deleted.

Loading

0 comments on commit e1f5409

Please sign in to comment.