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

Commit

Permalink
plugin: move hardware logics out from controller to plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
zllovesuki committed Dec 7, 2020
1 parent 1359209 commit 5d04716
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 124 deletions.
15 changes: 12 additions & 3 deletions controller/cmd.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package controller

import (
"github.com/zllovesuki/G14Manager/cxx/plugin/keyboard"
"github.com/zllovesuki/G14Manager/cxx/plugin/volume"
"github.com/zllovesuki/G14Manager/system/atkacpi"
"github.com/zllovesuki/G14Manager/system/battery"
kb "github.com/zllovesuki/G14Manager/system/keyboard"
"github.com/zllovesuki/G14Manager/system/persist"
"github.com/zllovesuki/G14Manager/system/plugin"
"github.com/zllovesuki/G14Manager/cxx/plugin/keyboard"
"github.com/zllovesuki/G14Manager/cxx/plugin/volume"
"github.com/zllovesuki/G14Manager/system/power"
"github.com/zllovesuki/G14Manager/system/thermal"

Expand Down Expand Up @@ -66,7 +67,15 @@ func New(conf RunConfig) (*Controller, error) {
return nil, err
}

kbCtrl, err := keyboard.NewControl(conf.DryRun)
remap := make(map[uint32]uint16)
if conf.EnabledFeatures.FnRemap {
remap[kb.KeyFnLeft] = kb.KeyPgUp
remap[kb.KeyFnRight] = kb.KeyPgDown
}
kbCtrl, err := keyboard.NewControl(keyboard.Config{
DryRun: conf.DryRun,
Remap: remap,
})
if err != nil {
return nil, err
}
Expand Down
23 changes: 10 additions & 13 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ const (
fnPersistConfigs = iota // for debouncing persisting to Registry
fnCheckCharger // for debouncing power input change acpi event
fnApplyConfigs // for loading and re-applying configurations
fnKbCtrl // for controlling keyboard behaviors
fnToggleTouchPad // for toggling touchpad enable/disable
fnVolCtrl // for mute/unmute microphone
fnHwCtrl // for notifying atkacpi
fnBeforeSuspend // for doing work before suspend
fnAfterSuspend // for doing work after suspend
Expand Down Expand Up @@ -85,9 +82,10 @@ type Controller struct {
workQueueCh map[uint32]workQueue
errorCh chan error

keyCodeCh chan uint32
acpiCh chan uint32
powerEvCh chan uint32
keyCodeCh chan uint32
acpiCh chan uint32
powerEvCh chan uint32
pluginCbCh chan plugin.Callback
}

func newController(conf Config) (*Controller, error) {
Expand All @@ -110,9 +108,10 @@ func newController(conf Config) (*Controller, error) {
workQueueCh: make(map[uint32]workQueue, 1),
errorCh: make(chan error),

keyCodeCh: make(chan uint32, 1),
acpiCh: make(chan uint32, 1),
powerEvCh: make(chan uint32, 1),
keyCodeCh: make(chan uint32, 1),
acpiCh: make(chan uint32, 1),
powerEvCh: make(chan uint32, 1),
pluginCbCh: make(chan plugin.Callback, 1),
}, nil
}

Expand Down Expand Up @@ -164,9 +163,6 @@ func (c *Controller) initialize(haltCtx context.Context) error {
workQueueImmediate := []uint32{
fnCheckCharger,
fnApplyConfigs,
fnToggleTouchPad,
fnKbCtrl,
fnVolCtrl,
fnHwCtrl,
fnBeforeSuspend,
fnAfterSuspend,
Expand Down Expand Up @@ -210,7 +206,7 @@ func (c *Controller) initialize(haltCtx context.Context) error {

func (c *Controller) startPlugins(haltCtx context.Context) {
for _, p := range c.Config.Plugins {
errChan := p.Run(haltCtx)
errChan := p.Run(haltCtx, c.pluginCbCh)
go func(ch <-chan error) {
for {
select {
Expand Down Expand Up @@ -245,6 +241,7 @@ func (c *Controller) Run(haltCtx context.Context) error {
c.startPlugins(haltCtx)

// defined in controller_loop.go
go c.handlePluginCallback(ctx)
go c.handleNotify(ctx)
go c.handleWorkQueue(ctx)
go c.handlePowerEvent(ctx)
Expand Down
69 changes: 23 additions & 46 deletions controller/controller_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ func (c *Controller) handleKeyPress(haltCtx context.Context) {
case kb.KeyFnV:
log.Println("hid: Fn + V Pressed")

case kb.KeyMuteMic:
log.Println("hid: mute/unmute microphone Pressed")
c.workQueueCh[fnVolCtrl].noisy <- struct{}{}

case kb.KeyTpadToggle:
log.Println("hid: toggle enable/disable touchpad Pressed")
c.workQueueCh[fnToggleTouchPad].noisy <- struct{}{}

case
kb.KeyLCDUp,
kb.KeyLCDDown,
Expand All @@ -107,11 +99,14 @@ func (c *Controller) handleKeyPress(haltCtx context.Context) {
c.workQueueCh[fnHwCtrl].noisy <- keyCode

case
kb.KeyMuteMic,
kb.KeyTpadToggle,
kb.KeyFnLeft,
kb.KeyFnRight,
kb.KeyFnUp,
kb.KeyFnDown:
c.workQueueCh[fnKbCtrl].noisy <- keyCode
log.Printf("hid: keyboard hardware function %+v\n", keyCode)
c.notifyPlugins(plugin.EvtKeyboardFn, keyCode)

default:
log.Printf("hid: Unknown %d\n", keyCode)
Expand Down Expand Up @@ -249,37 +244,6 @@ func (c *Controller) handleWorkQueue(haltCtx context.Context) {
Message: fmt.Sprintf("Current Thermal Plan: %s", c.Config.Thermal.CurrentProfile().Name),
}

case ev := <-c.workQueueCh[fnKbCtrl].clean:
switch ev.Data.(uint32) {
case kb.KeyFnLeft:
log.Println("kbCtrl: Fn + Arrow Left Pressed")
if c.Config.EnabledFeatures.FnRemap {
log.Println("kbCtrl: remapping to PgUp")
c.notifyPlugins(plugin.EvtKbEmulateKeyPress, kb.KeyPgUp)
}
case kb.KeyFnRight:
log.Println("kbCtrl: Fn + Arrow Right Pressed")
if c.Config.EnabledFeatures.FnRemap {
log.Println("kbCtrl: remapping to PgDown")
c.notifyPlugins(plugin.EvtKbEmulateKeyPress, kb.KeyPgDown)
}
case kb.KeyFnDown:
log.Println("kbCtrl: Decrease keyboard backlight")
c.notifyPlugins(plugin.EvtKbBrightnessDown, nil)
c.workQueueCh[fnPersistConfigs].noisy <- struct{}{}

case kb.KeyFnUp:
log.Println("kbCtrl: Increase keyboard backlight")
c.notifyPlugins(plugin.EvtKbBrightnessUp, nil)
c.workQueueCh[fnPersistConfigs].noisy <- struct{}{}
}

case <-c.workQueueCh[fnToggleTouchPad].clean:
c.notifyPlugins(plugin.EvtKbToggleTouchpad, nil)

case <-c.workQueueCh[fnVolCtrl].clean:
c.notifyPlugins(plugin.EvtVolToggleMute, nil)

case ev := <-c.workQueueCh[fnHwCtrl].clean:
keyCode := ev.Data.(uint32)
args := make([]byte, 8)
Expand All @@ -295,12 +259,11 @@ func (c *Controller) handleWorkQueue(haltCtx context.Context) {
}

case <-c.workQueueCh[fnBeforeSuspend].clean:
log.Println("kbCtrl: turning off keyboard backlight")
c.notifyPlugins(plugin.EvtKbBrightnessOff, nil)
c.notifyPlugins(plugin.EvtACPISuspend, nil)

case <-c.workQueueCh[fnAfterSuspend].clean:
log.Println("[controller] reinitialize kbCtrl and apply config")
c.notifyPlugins(plugin.EvtKbReInit, nil)
log.Println("[controller] re-apply config")
c.notifyPlugins(plugin.EvtACPIResume, nil)
c.workQueueCh[fnApplyConfigs].noisy <- struct{}{}

case <-haltCtx.Done():
Expand All @@ -310,12 +273,26 @@ func (c *Controller) handleWorkQueue(haltCtx context.Context) {
}
}

func (c Controller) notifyPlugins(evt plugin.Event, val interface{}) {
t := plugin.Task{
func (c *Controller) notifyPlugins(evt plugin.Event, val interface{}) {
t := plugin.Notification{
Event: evt,
Value: val,
}
for _, p := range c.Config.Plugins {
go p.Notify(t)
}
}

func (c *Controller) handlePluginCallback(haltCtx context.Context) {
for {
select {
case evt := <-c.pluginCbCh:
switch evt {
case plugin.CbPersistConfig:
c.workQueueCh[fnPersistConfigs].noisy <- struct{}{}
}
case <-haltCtx.Done():
return
}
}
}
Loading

0 comments on commit 5d04716

Please sign in to comment.