|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "./stadia" |
| 5 | + "./xbox" |
| 6 | + "github.com/getlantern/systray" |
| 7 | + "github.com/rodolfoag/gow32" |
| 8 | + "gopkg.in/toast.v1" |
| 9 | +) |
| 10 | + |
| 11 | +var stop = false |
| 12 | + |
| 13 | +func main() { |
| 14 | + _, err := gow32.CreateMutex("Stadia2Xbox") |
| 15 | + if err != nil { |
| 16 | + msg("An instance of Stadia2Xbox is already running!") |
| 17 | + } else { |
| 18 | + systray.Run(ready, close) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func ready() { |
| 23 | + icon, _ := Asset("data/stadia.ico") |
| 24 | + systray.SetIcon(icon) |
| 25 | + quit := systray.AddMenuItem("Exit", "Exits the application") |
| 26 | + |
| 27 | + go func() { |
| 28 | + <-quit.ClickedCh |
| 29 | + systray.Quit() |
| 30 | + }() |
| 31 | + |
| 32 | + start() |
| 33 | +} |
| 34 | + |
| 35 | +func start() { |
| 36 | + defer systray.Quit() |
| 37 | + |
| 38 | + device, err := stadia.Open() |
| 39 | + if err != nil { |
| 40 | + msg(err.Error()) |
| 41 | + return |
| 42 | + } |
| 43 | + defer device.Close() |
| 44 | + |
| 45 | + emu, err := xbox.Open(func(vibration xbox.Vibration) { |
| 46 | + device.Vibrate(vibration.LargeMotor, vibration.SmallMotor) |
| 47 | + }) |
| 48 | + if err != nil { |
| 49 | + msg(err.Error()) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + con, err := emu.Connect() |
| 54 | + if err != nil { |
| 55 | + msg(err.Error()) |
| 56 | + return |
| 57 | + } |
| 58 | + defer emu.Close() |
| 59 | + defer con.Close() |
| 60 | + |
| 61 | + msg("Stadia Controller sucessfully connected and emulated as Xbox Controller") |
| 62 | + |
| 63 | + for { |
| 64 | + if stop { |
| 65 | + return |
| 66 | + } |
| 67 | + d, err := device.Read() |
| 68 | + if err != nil { |
| 69 | + msg(err.Error()) |
| 70 | + return |
| 71 | + } |
| 72 | + report := xbox.Report{} |
| 73 | + |
| 74 | + report.SetButton(d.DPad.Up, xbox.DPadUp) |
| 75 | + report.SetButton(d.DPad.Down, xbox.DPadDown) |
| 76 | + report.SetButton(d.DPad.Left, xbox.DPadLeft) |
| 77 | + report.SetButton(d.DPad.Right, xbox.DPadRight) |
| 78 | + |
| 79 | + report.SetButton(d.Button.X, xbox.ButtonX) |
| 80 | + report.SetButton(d.Button.Y, xbox.ButtonY) |
| 81 | + report.SetButton(d.Button.A, xbox.ButtonA) |
| 82 | + report.SetButton(d.Button.B, xbox.ButtonB) |
| 83 | + |
| 84 | + report.SetButton(d.Button.Home, xbox.ButtonGuide) |
| 85 | + report.SetButton(d.Button.Menu, xbox.ButtonStart) |
| 86 | + report.SetButton(d.Button.Options, xbox.ButtonBack) |
| 87 | + |
| 88 | + report.SetButton(d.Stick.Left, xbox.StickLeft) |
| 89 | + report.SetButton(d.Stick.Right, xbox.StickRight) |
| 90 | + |
| 91 | + report.SetButton(d.Bumper.Left, xbox.BumperLeft) |
| 92 | + report.SetButton(d.Bumper.Right, xbox.BumperRight) |
| 93 | + |
| 94 | + report.SetStick(false, int16(d.Stick.Axis.Left.X), int16(d.Stick.Axis.Left.Y)) |
| 95 | + report.SetStick(true, int16(d.Stick.Axis.Right.X), int16(d.Stick.Axis.Right.Y)) |
| 96 | + |
| 97 | + report.SetTrigger(false, d.Trigger.Pressure.Left) |
| 98 | + report.SetTrigger(true, d.Trigger.Pressure.Right) |
| 99 | + |
| 100 | + con.Send(&report) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func close() { |
| 105 | + stop = true |
| 106 | +} |
| 107 | + |
| 108 | +func msg(str string) { |
| 109 | + notif := toast.Notification{ |
| 110 | + Title: "Stadia2Xbox", |
| 111 | + Message: str, |
| 112 | + } |
| 113 | + notif.Push() |
| 114 | +} |
0 commit comments