|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os/exec" |
| 9 | + |
| 10 | + "github.com/71/stadiacontroller" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + shell = flag.String("shell", "pwsh", "a path to the shell to execute for commands") |
| 15 | + |
| 16 | + onCapturePressed = flag.String("capture-pressed", "", "a command to run when the Capture button is pressed") |
| 17 | + onCaptureReleased = flag.String("capture-released", "", "a command to run when the Capture button is released") |
| 18 | + onAssistantPressed = flag.String("assistant-pressed", "", "a command to run when the Assistant button is pressed") |
| 19 | + onAssistantReleased = flag.String("assistant-released", "", "a command to run when the Assistant button is released") |
| 20 | +) |
| 21 | + |
| 22 | +func main() { |
| 23 | + flag.Parse() |
| 24 | + |
| 25 | + err := run() |
| 26 | + |
| 27 | + if err != nil { |
| 28 | + log.Fatal(err) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func run() error { |
| 33 | + controller := stadiacontroller.NewStadiaController() |
| 34 | + |
| 35 | + defer controller.Close() |
| 36 | + |
| 37 | + emulator, err := stadiacontroller.NewEmulator(func(vibration stadiacontroller.Vibration) { |
| 38 | + controller.Vibrate(vibration.LargeMotor, vibration.SmallMotor) |
| 39 | + }) |
| 40 | + |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("unable to start ViGEm client: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + defer emulator.Close() |
| 46 | + |
| 47 | + x360, err := emulator.CreateXbox360Controller() |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("unable to create emulated Xbox 360 controller: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + defer x360.Close() |
| 54 | + |
| 55 | + if err = x360.Connect(); err != nil { |
| 56 | + return fmt.Errorf("unable to connect to emulated Xbox 360 controller: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + assistantPressed, capturePressed := false, false |
| 60 | + |
| 61 | + for { |
| 62 | + report, err := controller.GetReport() |
| 63 | + |
| 64 | + if err != nil { |
| 65 | + if errors.Is(err, stadiacontroller.RetryError) { |
| 66 | + continue |
| 67 | + } |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + err = x360.Send(&report) |
| 72 | + |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + if report.Assistant != assistantPressed { |
| 78 | + assistantPressed = report.Assistant |
| 79 | + |
| 80 | + if err := runButtonPress(assistantPressed, *onAssistantPressed, *onAssistantReleased); err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if report.Capture != capturePressed { |
| 86 | + capturePressed = report.Capture |
| 87 | + |
| 88 | + if err := runButtonPress(capturePressed, *onCapturePressed, *onCaptureReleased); err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func runButtonPress(pressed bool, ifPressed, ifReleased string) error { |
| 96 | + if pressed && ifPressed != "" { |
| 97 | + return runCommand(ifPressed) |
| 98 | + } |
| 99 | + if !pressed && ifReleased != "" { |
| 100 | + return runCommand(ifReleased) |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func runCommand(cmd string) error { |
| 106 | + command := exec.Command(*shell, "/C", cmd) |
| 107 | + |
| 108 | + go func() { |
| 109 | + err := command.Wait() |
| 110 | + |
| 111 | + if err != nil { |
| 112 | + log.Printf("command '%s' failed: %v", cmd, err) |
| 113 | + } |
| 114 | + }() |
| 115 | + |
| 116 | + return command.Start() |
| 117 | +} |
0 commit comments