[go: up one dir, main page]

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

Commit

Permalink
make device dry-run instead
Browse files Browse the repository at this point in the history
  • Loading branch information
zllovesuki committed Oct 10, 2020
1 parent 54f2fa2 commit e052ae7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 63 deletions.
17 changes: 8 additions & 9 deletions controller/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,23 @@ func Run(ctx context.Context, conf RunConfig) error {
conf.RogRemap = []string{defaultCommandWithArgs}
}

var wmi atkacpi.WMI
wmi, err := atkacpi.NewWMI(conf.DryRun)
if err != nil {
return err
}

var config persist.ConfigRegistry
var err error

if conf.DryRun {
wmi, _ = atkacpi.NewDryWMI()
config, err = persist.NewDryRegistryHelper()
if err != nil {
return err
}
} else {
wmi, err = atkacpi.NewWMI()
if err != nil {
return err
}
config, _ = persist.NewRegistryHelper()
}

// TODO: make powercfg dryrun-able as well
powercfg, err := power.NewCfg()
if err != nil {
return err
Expand All @@ -71,12 +70,12 @@ func Run(ctx context.Context, conf RunConfig) error {
return err
}

kbCtrl, err := keyboard.NewControl()
kbCtrl, err := keyboard.NewControl(conf.DryRun)
if err != nil {
return err
}

volCtrl, err := volume.NewControl()
volCtrl, err := volume.NewControl(conf.DryRun)
if err != nil {
return err
}
Expand Down
33 changes: 0 additions & 33 deletions system/atkacpi/dry_wmi.go

This file was deleted.

8 changes: 6 additions & 2 deletions system/atkacpi/wmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ type atkWmi struct {
var _ WMI = &atkWmi{}

// NewWMI returns an WMI for evaluating WMI methods exposed by the ATKD ACPI device
func NewWMI() (WMI, error) {
device, err := device.NewControl(devicePath, ioctl.ATK_ACPI_WMIFUNCTION)
func NewWMI(dryRun bool) (WMI, error) {
device, err := device.NewControl(device.Config{
DryRun: dryRun,
Path: devicePath,
ControlCode: ioctl.ATK_ACPI_WMIFUNCTION,
})
if err != nil {
return nil, err
}
Expand Down
49 changes: 34 additions & 15 deletions system/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ import (
"golang.org/x/sys/windows"
)

type Config struct {
DryRun bool
Path string
ControlCode uint32
}

type Control struct {
path string
handle windows.Handle
controlCode uint32
Config
handle windows.Handle
}

func NewControl(path string, controlCode uint32) (*Control, error) {
if len(path) == 0 {
func NewControl(conf Config) (*Control, error) {
if len(conf.Path) == 0 {
return nil, errors.New("path cannot be empty")
}
if conf.ControlCode == 0 {
return nil, errors.New("control code cannot be 0")
}
h, err := windows.CreateFile(
windows.StringToUTF16Ptr(path),
windows.StringToUTF16Ptr(conf.Path),
// 0x80 is FILE_READ_ATTRIBUTES https://docs.microsoft.com/en-us/windows/win32/fileio/file-access-rights-constants
0x80|windows.GENERIC_READ|windows.GENERIC_WRITE|windows.SYNCHRONIZE,
windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE,
Expand All @@ -33,19 +41,22 @@ func NewControl(path string, controlCode uint32) (*Control, error) {
}

return &Control{
path: path,
handle: h,
controlCode: controlCode,
Config: conf,
handle: h,
}, nil
}

func (d *Control) Write(input []byte) (int, error) {
if d.Config.DryRun {
log.Printf("[dry run] device: %s (%d) write input buffer [0:8]: %+v\n", d.Config.Path, d.Config.ControlCode, input[0:8])
return len(input), nil
}
outBuf := make([]byte, 1024)
outBufWritten := uint32(0)
log.Printf("device: %s (%d) write input buffer [0:8]: %+v\n", d.path, d.controlCode, input[0:8])
log.Printf("device: %s (%d) write input buffer [0:8]: %+v\n", d.Config.Path, d.Config.ControlCode, input[0:8])
err := windows.DeviceIoControl(
d.handle,
d.controlCode,
d.Config.ControlCode,
&input[0],
uint32(len(input)),
&outBuf[0],
Expand All @@ -61,11 +72,15 @@ func (d *Control) Write(input []byte) (int, error) {
}

func (d *Control) Read(outBuf []byte) (int, error) {
if d.Config.DryRun {
log.Printf("[dry run] device: %s (%d) read input buffer [0:8]: %+v\n", d.Config.Path, d.Config.ControlCode, outBuf[0:8])
return 0, nil
}
outBufWritten := uint32(0)
log.Printf("device: %s (%d) read input buffer [0:8]: %+v\n", d.path, d.controlCode, outBuf[0:8])
log.Printf("device: %s (%d) read input buffer [0:8]: %+v\n", d.Config.Path, d.Config.ControlCode, outBuf[0:8])
err := windows.DeviceIoControl(
d.handle,
d.controlCode,
d.Config.ControlCode,
nil,
0,
&outBuf[0],
Expand All @@ -80,12 +95,16 @@ func (d *Control) Read(outBuf []byte) (int, error) {
}

func (d *Control) Execute(input []byte, outLen int) ([]byte, error) {
if d.Config.DryRun {
log.Printf("[dry run] device: %s (%d) execute input buffer [0:8]: %+v\n", d.Config.Path, d.Config.ControlCode, input[0:8])
return make([]byte, outLen), nil
}
outBuf := make([]byte, 1024)
outBufWritten := uint32(0)
log.Printf("device: %s (%d) execute input buffer: %+v\n", d.path, d.controlCode, input)
log.Printf("device: %s (%d) execute input buffer: %+v\n", d.Config.Path, d.Config.ControlCode, input)
err := windows.DeviceIoControl(
d.handle,
d.controlCode,
d.Config.ControlCode,
&input[0],
uint32(len(input)),
&outBuf[0],
Expand Down
8 changes: 6 additions & 2 deletions system/keyboard/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type Control struct {
}

// NewControl checks if the computer has the hid control interface, and returns a control interface if it does
func NewControl() (*Control, error) {
func NewControl(dryRun bool) (*Control, error) {
devices, err := usb.EnumerateHid(VendorID, ProductID)
if err != nil {
return nil, err
Expand All @@ -139,7 +139,11 @@ func NewControl() (*Control, error) {
return nil, fmt.Errorf("Keyboard control interface not found")
}

ctrl, err := device.NewControl(path, ioctl.HID_SET_FEATURE)
ctrl, err := device.NewControl(device.Config{
DryRun: dryRun,
Path: path,
ControlCode: ioctl.HID_SET_FEATURE,
})
if err != nil {
return nil, err
}
Expand Down
11 changes: 9 additions & 2 deletions system/volume/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import (
type Control struct {
sync.Mutex
deferFuncs []func()
dryRun bool
isMuted bool
// defaultOutput *wca.IMMDevice
defaultInput *wca.IMMDevice
}

func NewControl() (*Control, error) {
c := &Control{}
func NewControl(dryRun bool) (*Control, error) {
c := &Control{
dryRun: dryRun,
}

if err := c.checkMicrophoneMute(); err != nil {
return nil, err
Expand Down Expand Up @@ -111,6 +114,10 @@ func (c *Control) ToggleMicrophoneMute() error {
c.Lock()
defer c.Unlock()

if c.dryRun {
return nil
}

done, err := c.connect()
if err != nil {
return err
Expand Down

0 comments on commit e052ae7

Please sign in to comment.